Trung Tran
Trung Tran

Reputation: 13731

Rails 4.0 Collection Select

I am very new to Rails 4.0 and having issues using the collection select in my form. I have an association where a Contract has many Task Orders. When creating a new Task Order I want the form to have a DDL of Contracts to select from.

My contracts table has the following attributes: contractId contractName

My task_orders table has the following attributes: task_orderId contract_Id <--- this is the foreign key reference to the contracts table

I read up on the Rails API and my for currently looks like this:

<div class="field">
  <%= f.label :contract_Id %><br>
  <%= collection.select(:TaskOrder, :contract_Id, :Contract.all, :id, {}, {}) %>
</div>

Please help! Thanks!

Upvotes: 0

Views: 112

Answers (1)

Pavan
Pavan

Reputation: 33542

Have a try with this

<div class="field">
  <%= f.label :contract_Id %><br>
  <%= collection_select(:task_order,:contract_Id,Contract.all, :id, {}, {}) %>
</div>

You wrote :TaskOrder which is wrong.Model instance should be used as a first param,not the Class Name And also it is collection_select not collection.select

For more details,see this API

Update

When using with form helpers,you should use collection_select like this

<%= f.collection_select(:contract_id,Contract.all, :id,:id,{:include_blank => true}) %>

Upvotes: 1

Related Questions