How do I create a collection_select in the View of a model that belongs_to another?

Posted by Angela on Stack Overflow See other posts from Stack Overflow or by Angela
Published on 2010-05-12T00:38:00Z Indexed on 2010/05/12 0:44 UTC
Read the original article Hit count: 227

Filed under:

In the _form for creating a new Contact, I want to be able to create a drop-down which allows the User to select the Campaign the Contact will belong to.

In the controller, I created a collection called @campaigns.

And I tried to use the following but not getting it to work:

  <p>
    <%= f.label :campaign_id %><br />
    <%= f.collection_select(:contact, :campaign_id, @campaigns, :id, :name) %>
  </p>

Basically, I want to display the available :name of the campaigns, and then submit the campaign_id associated with the selected campaign to the Contact model when it gets saved.

© Stack Overflow or respective owner

Related posts about collections