Search code examples
ruby-on-railsformsselectactionviewargument-error

Wrong argument in select box collection


I'm developing accept button for the company to accept the student who have applied for the certain job. It is possible for the student to apply many jobs which owned by one company. I created accept model which belongs to student, company, and job models.

So, I want to list out all the jobs which student has applied to and pass it as the job_id when the company clicks the accept button. Here I have these partial form:

<%= form_for([@student, @student.accepts.new]) do |f| %>
  <%= f.hidden_field :student_id, value: @student.id %>
  <%= f.collection_select(:job_id, @student.jobs, :id)  %>
  <%= f.submit 'Accept', class: 'btn btn-success' %>
<% end %>

And here is what I want to achive in console:

> @student.accepts.new(student_id: 1, job_id: 1)
=> #<Accept id: 1, student_id: 1, company_id: 1, job_id: 1, created_at: nil, updated_at: nil>

The current partial form will generate this error:

ArgumentError in Students#show

wrong number of arguments (3 for 4..6)

with red highlight to this code line:

<%= f.collection_select(:job_id, @student.jobs, :id) %>

There must be something wrong with my select but can't figure out what's wrong with it.


Solution

  • I change this line of code:

    <%= f.collection_select(:job_id, @student.jobs, :id)  %>
    

    to

    <%= f.collection_select(:job_id, @student.jobs, :id, :job_title)  %>
    

    and now it's working. Just a silly mistake.