grouped_options_for_select

143 views
Skip to first unread message

Dave Lynch

unread,
Feb 12, 2010, 11:18:58 AM2/12/10
to rubyonra...@googlegroups.com
Hi

I have two ojects @Parent @Child that i want to nest together to create
a select list for form.

@Parent @Child where @Parent has_many:Childern and Child
belongs_to:parent

In the db I have a field within child table call parent_id

I am using a form_for(@Parent...

and am using

<optgroup label="Parent">
<option value="1">Child name</option>
<option value="2">Child name</option>
<option value="3">Child name</option>
</optgroup>
<optgroup label="Parent2">
<option value="1">Child Name</option>
<option value="2">Child name</option>
</optgroup>


<%=
grouped_options_for_select(@Parent,Child.find(params[:parent_id]),:id,:name)%>

any help would be greatfull
--
Posted via http://www.ruby-forum.com/.

Sharagoz --

unread,
Feb 12, 2010, 11:30:26 AM2/12/10
to rubyonra...@googlegroups.com
<%= select_tag(:child_id,
option_groups_from_collection_for_select(@parent, :children, :name, :id,
:name)) %>

Sharagoz --

unread,
Feb 12, 2010, 11:34:29 AM2/12/10
to rubyonra...@googlegroups.com
Since this you are using "form_for" you want to use "f.select" not
"select_tag" like in my example.

Dave Lynch

unread,
Feb 13, 2010, 10:20:46 AM2/13/10
to rubyonra...@googlegroups.com
Sharagoz -- wrote:
> Since this you are using "form_for" you want to use "f.select" not
> "select_tag" like in my example.

Thanks Sharagoz

I had tried
<% option_groups_from_collection_for_select(@parent, :childern, :name,
:id, :name) %>

But was getting a "undefined method `inject' for #<Parent:0xb6828c1c>"
error from that line of code.

Dave Lynch

unread,
Feb 15, 2010, 12:05:29 PM2/15/10
to rubyonra...@googlegroups.com
Dave Lynch wrote:
> Sharagoz -- wrote:
>> Since this you are using "form_for" you want to use "f.select" not
>> "select_tag" like in my example.
>
> Thanks Sharagoz
>
> I had tried
> <% option_groups_from_collection_for_select(@parent, :childern, :name,
> :id, :name) %>
>
> But was getting a "undefined method `inject' for #<Parent:0xb6828c1c>"
> error from that line of code.

@parents should be a collection, like from
Parent.find(:all)

I needed to add a @parents line to my parent controller.

Under new method

@parents = Parent.find(:all)

Reply all
Reply to author
Forward
0 new messages