adding a drop down menu to index view

26 views
Skip to first unread message

fugee ohu

unread,
May 28, 2017, 4:17:53 PM5/28/17
to Ruby on Rails: Talk
I wanna add a dropdown menu to my index view for the index action of my artists_controller Trying to use collection_select but it responds undefined method `collection_select' for nil:NilClass The idea is to list artists by genre

<div class="field"> <%= f.collection_select( :artist, :genre, Genre.all, :id, :name) %> </div>

Chris Lerum

unread,
May 28, 2017, 4:28:35 PM5/28/17
to Ruby on Rails: Talk
try <%= f.collection_select( :artist, :genre_id, Genre.all, :id, :name) %>

fugee ohu

unread,
May 28, 2017, 4:49:06 PM5/28/17
to Ruby on Rails: Talk
gets me the same error, the  object is plural @artists, there's no form_for @artists instead i'm using <%= form_tag(artists_path) do |f| %>

Colin Law

unread,
May 28, 2017, 4:58:13 PM5/28/17
to Ruby on Rails: Talk
How are you setting f?

Colin

fugee ohu

unread,
May 28, 2017, 6:26:02 PM5/28/17
to Ruby on Rails: Talk

Colin Law

unread,
May 29, 2017, 4:00:28 AM5/29/17
to Ruby on Rails: Talk
form_tag must be used inside a form, it does not create a form

Colin

>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/1340f13b-8e7c-4e81-a63d-e28833acf816%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Walter Lee Davis

unread,
May 29, 2017, 9:33:22 AM5/29/17
to rubyonra...@googlegroups.com

> On May 29, 2017, at 3:59 AM, Colin Law <cla...@gmail.com> wrote:
>
> On 28 May 2017 at 23:26, fugee ohu <fuge...@gmail.com> wrote:
>>
>>
>> On Sunday, May 28, 2017 at 4:58:13 PM UTC-4, Colin Law wrote:
>>>
>>> On 28 May 2017 at 21:17, fugee ohu <fuge...@gmail.com> wrote:
>>>> I wanna add a dropdown menu to my index view for the index action of my
>>>> artists_controller Trying to use collection_select but it responds
>>>> undefined
>>>> method `collection_select' for nil:NilClass The idea is to list artists
>>>> by
>>>> genre
>>>>
>>>> <div class="field"> <%= f.collection_select( :artist, :genre, Genre.all,
>>>> :id, :name) %> </div>
>>>
>>> How are you setting f?
>>>
>>> Colin
>>
>>
>> <%= form_tag(artists_path) do |f| %>
>
> form_tag must be used inside a form, it does not create a form
>
> Colin


No, form_tag creates a form tag, unbound to any model object.

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

I think where the OP may be getting off track is that the form created by form_tag doesn't have a bound object, so saying f.collection_select is sort of meaningless. That syntax is used when you bind the form to an object, such as when you use form_for(@foo). You can use collection_select with or without an object, in the latter form, you have to provide the object as the first argument, but when you use the f.collection_select syntax, that first argument becomes the second argument, and the first argument is picked up from the f reference.

I think, ultimately, that's what's going on here -- the entire set of arguments to the helper are being shifted left one step, and the meaningless reference to the form builder (without bound object) is being passed in to the tag helper.

Walter

Colin Law

unread,
May 29, 2017, 10:24:08 AM5/29/17
to Ruby on Rails: Talk
On 29 May 2017 at 14:32, Walter Lee Davis <wa...@wdstudio.com> wrote:
>
>> On May 29, 2017, at 3:59 AM, Colin Law <cla...@gmail.com> wrote:
>>
>> On 28 May 2017 at 23:26, fugee ohu <fuge...@gmail.com> wrote:
>>>
>>>
>>> On Sunday, May 28, 2017 at 4:58:13 PM UTC-4, Colin Law wrote:
>>>>
>>>> On 28 May 2017 at 21:17, fugee ohu <fuge...@gmail.com> wrote:
>>>>> I wanna add a dropdown menu to my index view for the index action of my
>>>>> artists_controller Trying to use collection_select but it responds
>>>>> undefined
>>>>> method `collection_select' for nil:NilClass The idea is to list artists
>>>>> by
>>>>> genre
>>>>>
>>>>> <div class="field"> <%= f.collection_select( :artist, :genre, Genre.all,
>>>>> :id, :name) %> </div>
>>>>
>>>> How are you setting f?
>>>>
>>>> Colin
>>>
>>>
>>> <%= form_tag(artists_path) do |f| %>
>>
>> form_tag must be used inside a form, it does not create a form
>>
>> Colin
>
>
> No, form_tag creates a form tag, unbound to any model object.

You are right of course. I am getting old :(

Colin

>
> http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag
>
> I think where the OP may be getting off track is that the form created by form_tag doesn't have a bound object, so saying f.collection_select is sort of meaningless. That syntax is used when you bind the form to an object, such as when you use form_for(@foo). You can use collection_select with or without an object, in the latter form, you have to provide the object as the first argument, but when you use the f.collection_select syntax, that first argument becomes the second argument, and the first argument is picked up from the f reference.
>
> I think, ultimately, that's what's going on here -- the entire set of arguments to the helper are being shifted left one step, and the meaningless reference to the form builder (without bound object) is being passed in to the tag helper.
>
> Walter
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2FCF702E-E978-429C-999C-34B20FD514AC%40wdstudio.com.
Reply all
Reply to author
Forward
0 new messages