Topic: Making name tags unique when using multiple date_select helpers

I am trying to use multiple date_select helpers on a page. Each date_select will be saved as a row to a database table.

The tag I'm currently using:

date_select "time_log", 'date'

Which generates this:

<select name="time_log[date(2i)]">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option selected="selected" value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="time_log[date(3i)]">
</select>
<select name="time_log[date(1i)]">

So this will be accessible in my controller as: params[:time_log][:date}

Unfortunately, only one of these suckers can be used on a page, and I need a bunch of them. What options does the helper support to make the generated html tag names unique?

Last edited by jed.hurt (2006-08-31 10:34:03)

I thought about how mothers feed their babies with tiny little spoons and forks, so I wondered what do Chinese mothers use. Toothpicks?

Re: Making name tags unique when using multiple date_select helpers

Does each date correlate to an attribute (column) of a model? If not, you should use select_date instead - I know, I don't like the confusing names either. If you go with this you can use the "prefix" parameter to distinguish the different dates:

select_date :prefix => 'some_date'
select_date :prefix => 'some_other_date'
# etc.

If you want to stick with the date_select method, you can change either of the two parameters to distinguish between them.

date_select "time_log_1", 'date'
date_select "time_log_2", 'date'
# or
date_select "time_log", 'date_1'
date_select "time_log", 'date_2'
# etc.

Last edited by ryanb (2006-08-30 19:45:53)

Railscasts - Free Ruby on Rails Screencasts

Re: Making name tags unique when using multiple date_select helpers

All of the date boxes will refer to the same column of the time log model. I am trying to save multiple tuples from a single form submission. Will select date with a prefix work for this? Ideally, I would like to loop over an array of dates and save each array element as a time_log entry

params[:time_logs].each do |tl|
  @work_order.time_logs << TimeLog.create(tl)
end

What would my select_date tags need to look like in order for the controller to make them accessible with that loop?

I thought about how mothers feed their babies with tiny little spoons and forks, so I wondered what do Chinese mothers use. Toothpicks?

Re: Making name tags unique when using multiple date_select helpers

Yeah, an array of param hashes would be an ideal solution in this case. You can create an array of values in the params by suffixing the name with [] like time_logs[]. But, the problem is this can only do an array of values, not an array of hashes, so you cannot do time_logs[][:date]. The work around is to put an integer in there like this: time_logs[1][:date]. It will no longer be an array of hashes, but a hash of hashes - with me so far? smile

Perhaps some code will help.

# controller
def new_time_logs
  @time_logs = Array.new(5) { TimeLog.new } # You can set default values here
end

def create_time_logs
  # ...
  params[:time_logs].each_value do |tl|
    @work_order.time_logs << TimeLog.create(tl)
  end
end

# new_time_logs.rhtml
<% @time_logs.each_with_index do |time_log, index| %>
  <% fields_for "time_logs[#{index}]", time_log do |f| %>
    <%= f.date_select 'some_date_column' %>
  <% end %>
<% end %>


Completely untested, so hopefully that works.

Last edited by ryanb (2006-08-31 12:18:53)

Railscasts - Free Ruby on Rails Screencasts