Jacob Patton
2009-May-13 06:43 UTC
form_for with multiple objects, each with a preset attribute?
I''m using Rails to create a schedule of a shop''s opening and closing hours. Simplified versions of the models I''ve set up is: class Account < ActiveRecord::Base has_many :opening_hours, :dependent => :destroy end class OpeningHour < ActiveRecord::Base belongs_to :account # table schema # create_table "opening_hours", :force => true do |t| # t.integer "account_id" # t.string "day_of_week" # t.string "begins" # t.string "ends" # end end So an account might have one opening_hour for Monday (:day_of_week => "Monday", :opens =>"9am", :closes => "5pm"), two for Tuesday ([:day_of_week => "Tuesday", :opens => "9am", :closes => "12pm"], [:day_of_week => "Tuesday", :opens => "1pm", :closes => "5pm"]), etc. As I begin work on the HTML form for this schedule, I realize I''m not sure of the best way to allow the user to add multiple objects within one form when each object already has an attribute defined (the day_of_week attr). The form I''m working on should look like this: Monday Opening Hours [begins text_input] - [ends text_input] Tuesday Opening Hours [begins text_input] - [ends text_input] [begins text_input] - [ends text_input] ... Does anyone know of a standard way to handle this sort of requirement? I''m familiar with this method[1] to add multiple child objects in a single form, but in Ryan''s example, the tasks lack any pre-determined attribute values. [1]: http://railscasts.com/episodes/73-complex-forms-part-1 Thanks very much for your help, Jacob Patton
Jacob Patton
2009-May-18 07:30 UTC
Re: form_for with multiple objects, each with a preset attribute?
I''ve got this figured out now. I used a nested object form[1] to handle creating/updating multiple OpeningHour records: [1]: http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes class Account < ActiveRecord::Base has_many :opening_hours, :dependent => :destroy # Add attributes for each day of the week days_of_week = %w(Monday Tuesday Wednesday Thursday Friday Saturday Sunday) days_of_week.each do |day_of_week| attribute = "#{day_of_week.downcase}_opening_hours".to_sym has_many attribute, :class_name => "OpeningHour", :conditions => {:day_of_week => day_of_week} accepts_nested_attributes_for attribute, :allow_destroy => true attr_accessible (attribute.to_s + "_attributes").to_sym end end In the template, loop through the available fields by using fields_for: <%- form_for account do |f| %> <% f.fields_for :monday_opening_hours do |opening_hour| %> <%= opening_hour.text_field :begins %> to <%= opening_hour.text_field :ends %> <% end %> ... <%- end %> -Jacob Patton On May 13, 9:43 am, Jacob Patton <jacob.pat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m using Rails to create a schedule of a shop''s opening and closing > hours. Simplified versions of the models I''ve set up is: > > class Account < ActiveRecord::Base > > has_many :opening_hours, :dependent => :destroy > > end > > class OpeningHour < ActiveRecord::Base > > belongs_to :account > > # table schema > # create_table "opening_hours", :force => true do |t| > # t.integer "account_id" > # t.string "day_of_week" > # t.string "begins" > # t.string "ends" > # end > > end > > So an account might have one opening_hour for Monday (:day_of_week => > "Monday", :opens =>"9am", :closes => "5pm"), two for Tuesday > ([:day_of_week => "Tuesday", :opens => "9am", :closes => "12pm"], > [:day_of_week => "Tuesday", :opens => "1pm", :closes => "5pm"]), etc. > > As I begin work on the HTML form for this schedule, I realize I''m not > sure of the best way to allow the user to add multiple objects within > one form when each object already has an attribute defined (the > day_of_week attr). > > The form I''m working on should look like this: > > Monday Opening Hours > > [begins text_input] - [ends text_input] > > Tuesday Opening Hours > > [begins text_input] - [ends text_input] > > [begins text_input] - [ends text_input] > ... > > Does anyone know of a standard way to handle this sort of > requirement? I''m familiar with this method[1] to add multiple child > objects in a single form, but in Ryan''s example, the tasks lack any > pre-determined attribute values. > > [1]:http://railscasts.com/episodes/73-complex-forms-part-1 > > Thanks very much for your help, > > Jacob Patton
elioncho
2012-Nov-25 03:19 UTC
Re: form_for with multiple objects, each with a preset attribute?
This was really useful. Thanks for leaving info on how you did it. On Wednesday, May 13, 2009 1:43:10 AM UTC-5, Jacob Patton wrote:> > I''m using Rails to create a schedule of a shop''s opening and closing > hours. Simplified versions of the models I''ve set up is: > > class Account < ActiveRecord::Base > > has_many :opening_hours, :dependent => :destroy > > end > > class OpeningHour < ActiveRecord::Base > > belongs_to :account > > # table schema > # create_table "opening_hours", :force => true do |t| > # t.integer "account_id" > # t.string "day_of_week" > # t.string "begins" > # t.string "ends" > # end > > end > > So an account might have one opening_hour for Monday (:day_of_week => > "Monday", :opens =>"9am", :closes => "5pm"), two for Tuesday > ([:day_of_week => "Tuesday", :opens => "9am", :closes => "12pm"], > [:day_of_week => "Tuesday", :opens => "1pm", :closes => "5pm"]), etc. > > As I begin work on the HTML form for this schedule, I realize I''m not > sure of the best way to allow the user to add multiple objects within > one form when each object already has an attribute defined (the > day_of_week attr). > > The form I''m working on should look like this: > > Monday Opening Hours > > [begins text_input] - [ends text_input] > > Tuesday Opening Hours > > [begins text_input] - [ends text_input] > > [begins text_input] - [ends text_input] > ... > > Does anyone know of a standard way to handle this sort of > requirement? I''m familiar with this method[1] to add multiple child > objects in a single form, but in Ryan''s example, the tasks lack any > pre-determined attribute values. > > [1]: http://railscasts.com/episodes/73-complex-forms-part-1 > > Thanks very much for your help, > > Jacob Patton > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/WfMWSnETLBUJ. For more options, visit https://groups.google.com/groups/opt_out.