Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use collection_select syntax?
    primarykey
    data
    text
    <p>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.</p> <p>I want to be able to create the 'mandatory' relationship by selecting the names of course and departments instead of their id.</p> <p>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 <code>collection_select</code> helper method. I have read the documentation and I don't understand it.</p> <p>So my Department Model is</p> <pre><code>class Department &lt; ActiveRecord::Base attr_accessible :industry_name, :name has_many :employees has_many :mandatories has_many :courses, :through =&gt; :mandatories end </code></pre> <p>Course Model</p> <pre><code>class Course &lt; ActiveRecord::Base attr_accessible :name has_many :registrations has_many :mandatories has_many :employees, :through =&gt; :registrations has_many :departments, :through =&gt; :mandatories end </code></pre> <p>Mandatory Model</p> <pre><code>class Mandatory &lt; ActiveRecord::Base attr_accessible :course_id, :department_id belongs_to :course belongs_to :department end </code></pre> <p>The syntax I got from the documentation is <code>&lt;%= collection_select(:person, :city_id, City.all, :id, :name) %&gt;</code></p> <p>Do not understand this at all. </p> <p>EDIT::</p> <p>So, heres is my current view. I have no <code>collection_select</code> 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. </p> <p>This is my <code>_form.html</code> for Mandatory Controller</p> <pre><code>&lt;%= form_for(@mandatory) do |f| %&gt; &lt;% if @mandatory.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@mandatory.errors.count, "error") %&gt; prohibited this mandatory from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @mandatory.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :department_id %&gt;&lt;br /&gt; &lt;%= f.number_field :department_id %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :course_id %&gt;&lt;br /&gt; &lt;%= f.number_field :course_id %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>And here is Mandatory Controller </p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload