ruby-on-rails - Cocoon - 用于查找或创建的参数数量错误(1 代表 0):belongs_to

标签 ruby-on-rails ruby forms cocoon-gem

按照 Cocoon wiki 实现 The look-up or create :belongs_to我收到错误消息:参数数量错误(1 代表 0)。除了使用 slim 作为我的预编译器之外,我不完全确定它指的是我逐字遵循教程。这是我的代码的样子:

模型

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

项目表格

<%= 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 %>

项目总监

 ...

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

回溯

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'

最佳答案

ActiveRecord#all 已在 rails 4 中更改 - 现在正在做 scoped 过去所做的事情。它不需要任何额外的参数。而不是 User.all(order: 'name') 做:

User.order(:name)

关于ruby-on-rails - Cocoon - 用于查找或创建的参数数量错误(1 代表 0):belongs_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26804199/

相关文章:

ruby-on-rails - 了解 Rails 2.3 迁移备忘单吗?

ruby-on-rails - button_to 与 :remote=>true doesn't update the vote count on my page

ruby-on-rails - sleep 在 sidekiq worker 中是否正常工作?

arrays - 在 Ruby 中创建具有开始值、结束值和浮点值步长的数组

ruby - 用于字段比较的 watir-webdriver 迭代表

PHP输出单引号和双引号作为输入元素的值

ruby-on-rails - 关系和setter方法

mysql - 将动态代码传递给 MySQL 执行

javascript - 提交 AJAX 表单旧样式

html - 停止表单输入框自动完成的正确方法是什么?