Multiple text fields all with the same method

I have 3 text fields in a form , all with the same method, since those params will be inserted via a loop which is set up in the create action. However, I’m noticing that the form is only sending through the 1st value and leaving out the additional 2.

In my form I have this: <%= text_field(:cantitle, :title_opt)%> <%= text_field(:cantitle, :title_opt)%> <%= text_field(:cantitle, :title_opt)%>

Even though I put input into each , the params came through as such:

{“cantitle”=>{“title_opt”=>“CEO”}, # so the 2nd and 3rd were skipped.

In the controller I have this loop- # which if 3 values were submitted , 3 records would be created. a = (params[:cantitle][:title_opt]) a.each {|x| cantitle = Cantitle.new cantitle.title_opt = x cantitle.save }

Any ideas how to get each value through ? Also, I’m wondering what would happen if someone filled in the 1st and 3rd box and left the 2nd blank. Do I have to account for a null value ?

Stuart

It is not possible to have same name for all the fields, that’s why only one field is getting populated.

Bala, thanks - you always come through for me. Actually though it is possible (as I just learned) by adding “index” = x

So now I have the text fields as such: <%= text_field(:cantitle, :title_opt, “index” => 1)%>

<%= text_field(:cantitle, :title_opt, “index” => 2)%> <%= text_field(:cantitle, :title_opt, “index” => 3)%>

Which get translated into : <input id=“canlocation_1_city” name=“canlocation[1][city]” size=“30” type=“text” /

And params are such: “cantitle”=>{“1”=>{“title_opt”=>“CEO”}, “2”=>{“title_opt”=>“COO”}, “3”=>{“title_opt”=>“CIO”}},

Stuart


Great, looks like we can learn a lot from the Rails API.