Forum: Ruby on Rails form_for with multiple objects, each with a preset attribute?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B4a04ecbe2386b483c36695938f1c320?d=identicon&s=25 Jacob Patton (Guest)
on 2009-05-13 08:44
(Received via mailing list)
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
B4a04ecbe2386b483c36695938f1c320?d=identicon&s=25 Jacob Patton (Guest)
on 2009-05-18 09:31
(Received via mailing list)
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...

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
A1247f73598357f06388e863c41aa624?d=identicon&s=25 Elias Orozco (elioncho)
on 2012-11-25 04:21
(Received via mailing list)
This was really useful. Thanks for leaving info on how you did it.
This topic is locked and can not be replied to.