I’m having problems pulling up an edit page with 3 text fields.
The text fields look like such -
<%= text_field(:cantitle, :title_opt, “index” => 1)%>
<%= text_field(:cantitle, :title_opt, “index” => 2)%>
bump
Still haven’t figured out how to pull the values into the update form.
Adding map
@cantitle = Cantitle.find(:all, :conditions => [“candidate_id = ?”, @candidate_id]).map {|x| x.title_opt}
still throws an exception
undefined method title_opt’ for [“Channel manager”, “Regional Sales manager”, “Sales manager”]:Array
I know title_opt is not undefined,I’ve gone over all the association thrice.
I’m sure it has something to do with the way I’m pulling the array out of the database
I’m going to start this message over again since I may not have explained it well the first time.
I have multiple text_field(s) in my edit form.
Each one is for the same model method, however each field has a unique index number .
My problem is getting the records from the database into the text_fields.
These are the fields:
<%= text_field(:cantitle, :title_opt, “index” => 1)%>
<%= text_field(:cantitle, :title_opt, “index” => 2)%>
I can fill an array in the controller with
@cantitle = Cantitle.find(:all, :conditions => [“candidate_id = ?”, @candidate_id]).map {|x| x.title_opt}
What I can’t figure out is how to loop it correctly so that each text field gets one of the values.
Right now the find command throws an exceptionundefined methodtitle_opt’ for [“Channel manager”, “Regional Sales manager”, “Sales manager”]:Array
<%= text_field_tag(“cheese[#{cheese.id}]”,
cheese.name) %>
I guess I was thinking about the warning not to be the values directly into the conditions portion of a sql statement. Not sure if having them in the view is the same.
Anyway I think I worked it out using
<% @canlocations.each_with_index do |canlocation, indx| %>
<%= text_field_tag “canlocation[#{indx}][city]”,
canlocation.city %><% end %>
My problem is getting the records from the database into the
text_fields.
These are the fields:
<%= text_field(:cantitle, :title_opt, "index" => 1)%>
<%= text_field(:cantitle, :title_opt, "index" => 2)%>
<%= text_field(:cantitle, :title_opt,"index" => 3)%>
I don't believe this is possible with the text_field helper. text_field
expects a symbol corresponding to a single model object to be passed as
the first parameter, and you are passing it a symbol corresponding to an
array object. text_field just isn't built to handle editing of multiple
records in the same view. (Someone please correct me if I'm off base
here.)
When I had to do something similar in one of my applications, I did it
like this:
<% @cheeses.each do |cheese| %>
<%= text_field_tag("cheese[#{cheese.id}]", cheese.name) %>
<% end %>
I'm not sure if there's a better way to do it, but this did work for me.
Hope this gives you some ideas!
There's also:
<% @cheeses.each do |@cheese| %>
<%= text_field "cheese", "name" %>
<% end %>
Can I kick it up a notch,
I want to do the same but with a select list
Example:
I have a collection_select that looks like this
<%= collection_select(:canlocation, :state_id, @states, :id, :name)
Similar to the text_field_tag above, there are multiple records in the users canlocations.
In the controller I have this collection:
@canlocation_options = Canlocation.find(:all,:conditions => [“candidate_id = ?”, @candidate_id]).map {|x| x.state_id}
Now I’d like to map the canlocation_options to the collection_select. When the @states list is displayed it’s on the previously selected state.
I thought perhaps this would work:
<% @canlocation_options.each_with_index do |canlocation_options, indx| %>