Search code examples
ruby-on-railsrubyformscocoon-gem

Cocoon - Wrong number of arguments (1 for 0) for look-up or create :belongs_to


Following the Cocoon wiki for implementing The look-up or create :belongs_to I'm receiving the error: wrong number of arguments (1 for 0). I'm not exactly sure what its referring to being that I'm following the tutorial verbatim aside from using slim as my precompiler. Here's what my code looks like:

Models

class Project < ActiveRecord::Base
    belongs_to :user
    has_many :tasks
    accepts_nested_attributes_for :tasks, :reject_if => :all_blank, :allow_destroy => true
    accepts_nested_attributes_for :user, :reject_if => :all_blank
end

class User < ActiveRecord::Base
  has_many :projects
end

Projects Form

<%= simple_form_for @project do |f| %>
  <%= f.input :name %>
  <%= f.input :description %>
  <h3>Tasks</h3>
  <div id="tasks">
    <%= f.simple_fields_for :tasks do |task| %>
      <%= render 'task_fields', :f => task %>
    <% end %>
    <div class="links">
      <%= link_to_add_association 'add task', f, :tasks %>
    </div>
  </div>

  <div id="user">
    <div id="user_from_list">
      <%= f.association :user, collection: User.all(:order => 'name'),  :prompt => 'Choose an existing user' %>
    </div>
    <%= link_to_add_association 'add a new person as owner', f, :user %>
  </div>
  <%= f.submit %>
<% end %>

Projects Controller

 ...

 def project_params
   params.require(:project).permit(:name, :description, tasks_attributes: [:id, :description, :done, :_destroy], user_attributes: [:id, :name])
 end

Backtrace

app/views/projects/_form.html.erb:16:in `block in _app_views_projects__form_html_erb___3132123068035883478_70337216288160'
app/views/projects/_form.html.erb:1:in `_app_views_projects__form_html_erb___3132123068035883478_70337216288160'
app/views/projects/new.html.erb:3:in `_app_views_projects_new_html_erb__2418839848133678570_70337176808940'

Solution

  • ActiveRecord#all has been changed in rails 4 - this is now doing what scoped used to do. It does not expect any extra params. Instead of User.all(order: 'name') do:

    User.order(:name)