gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:15:34 25 4
gpt4 key购买 nike

按照 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com