adding a drop down menu to index view

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

<%= f.collection_select( :artist, :genre, Genre.all, :id, :name) %>

try <%= f.collection_select( :artist, :genre_id, Genre.all, :id, :name) %>

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

How are you setting f?

Colin

<%= form_tag(artists_path) do |f| %>

> 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

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 :frowning:

Colin