nupac
nupac

Reputation: 2489

How do I use collection_select syntax?

I have a Department model, Course model and Mandatory model. Each department has some courses that are mandatory, so I use the Mandatory model to map between Courses and Department model.

I want to be able to create the 'mandatory' relationship by selecting the names of course and departments instead of their id.

I figured I would create 2 instance variables @departments(Department.all) and @courses(Course.all) and in the view I would show them as drop down so you choose 1 department, 1 course and create the Mandatory relationship. Turns out it's not as simple as I would have liked it to be. I do not understand how am I supposed to use the collection_select helper method. I have read the documentation and I don't understand it.

So my Department Model is

class Department < ActiveRecord::Base
attr_accessible :industry_name, :name

has_many :employees
has_many :mandatories
has_many :courses, :through => :mandatories
end

Course Model

class Course < ActiveRecord::Base
attr_accessible :name

has_many :registrations
has_many :mandatories
has_many :employees, :through => :registrations
has_many :departments, :through => :mandatories
end

Mandatory Model

class Mandatory < ActiveRecord::Base
attr_accessible :course_id, :department_id

belongs_to :course
belongs_to :department
end

The syntax I got from the documentation is <%= collection_select(:person, :city_id, City.all, :id, :name) %>

Do not understand this at all.

EDIT::

So, heres is my current view. I have no collection_select yet. right now to create a relationship between a Department and a Course I need to enter their ids which I don't want to do. I would like a drop down with department name and course name instead.

This is my _form.html for Mandatory Controller

<%= form_for(@mandatory) do |f| %>
<% if @mandatory.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@mandatory.errors.count, "error") %> prohibited this mandatory from being saved:</h2>

  <ul>
  <% @mandatory.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>

<div class="field">
  <%= f.label :department_id %><br />
  <%= f.number_field :department_id %>
</div>
<div class="field">
  <%= f.label :course_id %><br />
  <%= f.number_field :course_id %>
</div>

<div class="actions">
  <%= f.submit %>
</div>
<% end %>

And here is Mandatory Controller

def create
@mandatory = Mandatory.new(params[:mandatory])
@courses = Course.all
@departments  =Department.all
respond_to do |format|
  if @mandatory.save
    format.html { redirect_to @mandatory, notice: 'Mandatory was successfully created.' }
    format.json { render json: @mandatory, status: :created, location: @mandatory }
  else
    format.html { render action: "new" }
    format.json { render json: @mandatory.errors, status: :unprocessable_entity }
  end
end
end

Upvotes: 0

Views: 1379

Answers (2)

Matt
Matt

Reputation: 14038

You need to use it in the context of a form like so:

<% form_for @mandatory do |f| %>

  <div class="field">
    <%= f.label :course_id %><br />
    <%= f.collection_select(:course_id, Course.all, :id, :name) %>
  </div>

  <%= f.submit %>
<% end %>

Upvotes: 1

Fred
Fred

Reputation: 8602

<%= collection_select(:person, :city_id, City.all, :id, :name) %>

Let's look at the parts:

:person is the name of the model that contains one item from the collection.

:city_id is the name of the field in the model that contains the item from the collection.

City.all is the list of items in the collection. This could be an instance variable (@cities) if you did the database request in the controller (@cities = City.all). You can also fetch just a subset of the records if that is all you need in the collection (@cities = City.where("state = ?", ["NY", "SC", "NE"]))

:id and :name are the fields in the City object that are used in each line if the selection list. :id is the value and :name is the text for each option in the dropdown list.

The HTML generated should look like

<select name="person[city_id]">
<option value="1">New York</option>
<option value="3">Rome</option>
<option value="4">Charleston</option>
<option value="17">Omaha</option>
</select>

EDIT: the 'id' in select seems to have dropped out when I wasn't looking, so I have removed it.

Upvotes: 1

Related Questions