Display collection stuff

Hi, I am a bit confused on exactly how this works out.
I have ObjectA which has_many :things and Thing belongs_to :object_a

So, what I want to do is display all the things that ObjectA has in a
text_area. When I have

<%= f.text_area(“things”) %>

I get a text_area with

#Thing:0x460efb0#Thing:0x460ef38

I want to display thing.name, but I’m stuck at this point.

Any help would be great, thanks.

~Jeremy

Your code makes Rails do object_a.things and put the result in the
text area. when outputting an object like a string, ruby prints out
what you see, class name and the (internal ruby) object’s id: #<Thing:
0x460efb0>

you will have to do something like this:
<%= text_area_tag “things”, @object_a.things.map{ |t| t.name }.join("
") %>

then, when submitting the form, the content will be in params[:things]
if however you want it in params[:object_a][:things], change it like
this:

<%= text_area_tag “object_a[things]”, @object_a.things.map{ |t|
t.name }.join(" ") %>

be aware that this could lead to problems when doing mass assignment:
@object_@ =Object_a.new(params[:object]
=> “things” is no attribute of object_a (it’s only an associtiaon),
and produce an error.
you can however catch this with a virtual attribute…

On 17 Okt., 00:50, Jeremy W. [email protected]

Thorsten wrote:

you will have to do something like this:
<%= text_area_tag “things”, @object_a.things.map{ |t| t.name }.join("
") %>

then, when submitting the form, the content will be in params[:things]
if however you want it in params[:object_a][:things], change it like
this:

<%= text_area_tag “object_a[things]”, @object_a.things.map{ |t|
t.name }.join(" ") %>

Thanks, it didn’t work, but I did figure out what I needed.

<%= f.text_area “things”, :value => @object_a.things.map{ |t| t.name
}.join(" ") %>

~Jeremy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs