Render :collection and default value

Hi all,

I am having a lot of trouble to get the default value of text fields
when they are generated by a partial from a collection.

I have been on this for a whole week and I didnt find a single exemple
on the internet of someone using a partial that generate text field with
a default value.

I cant believe that I am the only one having this problem. Anyone has an
exemple they can show me? Or the explanation why the text fields dont
want to be automaticaly populated by the values from the collection?

Thanks a lot!

On 4/16/06, Alain [email protected] wrote:

exemple they can show me? Or the explanation why the text fields dont
want to be automaticaly populated by the values from the collection?

Thanks a lot!

Could you show us some code that isn’t working?

Thanks for you interest in my problem/ignorance Wilson,

Could you show us some code that isn’t working?

Sure, here it is:

Code from the controler:

def list
@active_client = session[:active_client]
@clients = Client.find_all.map {|c| [c.name, c.id] }
@variations = Variation.find(:all,
:conditions =>[“client_id = ?”,
@active_client.id]
)
end

Code from the view <list.rhtml>:

<%= start_form_tag (:action =>“save_list”)%>
<%= render(:partial => “variation”,
:collection => @variations )%>

<input type="submit" value="Sauvegarder" />

<%= end_form_tag %>

Now the partial <_variation.rhtml>:

concept:<%= h(variation.concept.name) %> variation francaise:<%= text_field "variation", "text_fr","index" => variation.id %> variation anglaise:<%= text_field "variation", "text_en","index" => variation.id %>

Need anything else?

This code result in a page where I can enter and save successfully new
data (French and English variations of a concept for a client), but when
I go on the page, the fields are not prepopulated by the data from the
database.

Thanks for you help!

On 4/16/06, Alain [email protected] wrote:

@clients = Client.find_all.map {|c| [c.name, c.id] }
                       :collection => @variations )%>

variation.id %>

Need anything else?

This code result in a page where I can enter and save successfully new
data (French and English variations of a concept for a client), but when
I go on the page, the fields are not prepopulated by the data from the
database.

Try putting this at the top of _variation.rhtml:
<% @variation = variation -%>
The form helpers accept strings and symbols, rather than the actual
objects. They then use instance_variable_get to fetch the object they
need.
However, in this case, there wasn’t an instance variable called
@variation, so you need to set one in the partial.

I find it to be a bit of a rough edge, but at least it’s only one
extra line of code.

Also, the partial-with-collection process will make a local variable
“variation_counter” available, and you can use that for the index,
rather than saying “variation.id”. That will make the numbering a
little more normal.

Just a hunch here, but try adding a line like this to the start of your
partial

<% @variation = variation %>

and then reference @variation instead of ‘variation’ in the rest of your
partial. I vaguely recall that some of the form helpers have issues if
the passed object is not an instance variable.

Warning: This could be entirely incorrect.

On Monday, April 17, 2006, at 4:00 AM, Alain wrote:

@clients = Client.find_all.map {|c| [c.name, c.id] }
:collection => @variations )%>
variation.id %>

Thanks for you help!


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin

Hi Wilson,

Try putting this at the top of _variation.rhtml:
<% @variation = variation -%>
The form helpers accept strings and symbols, rather than the actual
objects. They then use instance_variable_get to fetch the object they
need.
However, in this case, there wasn’t an instance variable called
@variation, so you need to set one in the partial.

That solved my problem! Thanks!

I find it to be a bit of a rough edge, but at least it’s only one
extra line of code.

Also, the partial-with-collection process will make a local variable
“variation_counter” available, and you can use that for the index,
rather than saying “variation.id”. That will make the numbering a
little more normal.

But arent the index supposed to be the id of the object? I tried to
replace the variation.id by variation_counter and got an error message
that it couldnt find a ‘variation’ with ID=0. This error is caused by
one of my other action (save) that fetch the variation that need to be
saved using the variation_id (index). That is why I think I am stuck
with using the current code. Unless I missed something…

Thanks for all the help! Now back on development!