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)%>
<%= text_field(:cantitle, :title_opt,“index” => 3)%>
In my controller - @cantitles = Cantitle.find(:all, :conditions =>
[“candidate_id = ?”, @candidate_id])
I thought perhaps I could use the instance variable @cantitles in the
text_field elements , but not enough arguments.
Not sure how to go about this.
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)%>
<%= text_field(:cantitle, :title_opt,“index” => 3)%>
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 exception
undefined method `title_opt’ for [“Channel manager”, “Regional Sales
manager”, “Sales manager”]:Array
Well for me it doesn’t work, but not sure if I should pursue it , since
I
believe it’s not sql safe.
um
what
SQL isn’t generated by view helpers; it’s generated by ActiveRecord
calls from your controller. I’m not sure what you mean by “not sql safe”
in this context.
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!
<%= 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 %>
SQL isn’t generated by view helpers; it’s generated by ActiveRecord
calls from your controller. I’m not sure what you mean by “not sql safe”
in this context.
That works well also,
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|
%>
<%= collection_select(:canlocation, :state_id, @states, :id, :name,
:selected => “canlocation_options[#{indx}][state_id]”)
canlocation_options.state_id %>
<% end %>
My problem is getting the records from the database into the
the first parameter, and you are passing it a symbol corresponding to
<% @cheeses.each do |cheese| %>
<%= text_field “cheese[]”, “name” %>
<%= collection_select(:canlocation, :state_id, @states, :id, :name)
I thought perhaps this would work: