edit / update text_field

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.

Stuart

Changed controller code to @cantitle = Cantitle.find(:all, :conditions => [“candidate_id = ?”, @candidate_id])

undefined method title_opt’ for #Array:0x485e760

Stuart
`

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

Stuart
`

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 exceptionundefined methodtitle_opt’ for [“Channel manager”, “Regional Sales manager”, “Sales manager”]:Array

Hope this is clear.

TIA
Stuart
`

Well for me it doesn’t work, but not sure if I should pursue it , since I believe it’s not sql safe.

Stuart

<%= 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 %>

Which is similar to what you had suggested.

Stuart

Hi --

Stuart Fellowes wrote:

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 %>

which will give you a params hash like this:

   "cheese" => { "12" => { "name" => "brie" },
                 "33" => { "name" => "cheddar" }
               ... }

where the integers are the ids of the records.

David

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 %>

ndefined method `state_id' for 12:Fixnum

I’m open to suggestions.
Stuart

I solved the collection_select by doing this:

@canlocation_options = Canlocation.find(:all,:conditions => [“candidate_id = ?”, @candidate_id]).map {|x| x.state_id}

Then in the form:

<%= options_from_collection_for_select @states, :id, :name, @canlocation_options[0] %>
<%= options_from_collection_for_select @states, :id, :name, @canlocation_options[1] %>

<%= options_from_collection_for_select @states, :id, :name, @canlocation_options[2] %>