Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我得到一个未定义的方法“link”to“add”association';对于#<#&书信电报;类别:0x00007fb9a1cc4f68>;:0x00007fb9a1fdf658>;使用创业板时“;“蚕茧”;_Ruby On Rails - Fatal编程技术网

Ruby on rails 我得到一个未定义的方法“link”to“add”association';对于#<#&书信电报;类别:0x00007fb9a1cc4f68>;:0x00007fb9a1fdf658>;使用创业板时“;“蚕茧”;

Ruby on rails 我得到一个未定义的方法“link”to“add”association';对于#<#&书信电报;类别:0x00007fb9a1cc4f68>;:0x00007fb9a1fdf658>;使用创业板时“;“蚕茧”;,ruby-on-rails,Ruby On Rails,我正在尝试用gemcook构建一个嵌套表单,我遵循这个()教程,因为它与我打算做的几乎相同。简而言之,我想创建一个表单,用户可以在其中创建一个列表,然后根据自己的意愿向该列表中添加任意多的项目 但是,我为#得到了一个未定义的方法“link_to_add_association” 型号 class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :reme

我正在尝试用gemcook构建一个嵌套表单,我遵循这个()教程,因为它与我打算做的几乎相同。简而言之,我想创建一个表单,用户可以在其中创建一个列表,然后根据自己的意愿向该列表中添加任意多的项目

但是,我为#得到了一个未定义的方法“link_to_add_association”

型号

class User < ApplicationRecord
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
has_many :lists, dependent: :destroy
end

class List < ApplicationRecord
belongs_to :user
has_many :items, dependent: :destroy
accepts_nested_attributes_for :items, allow_destroy: true, reject_if: 
proc { |att| att['description'].blank?}
end

class Item < ApplicationRecord
belongs_to :list, optional: true
end
class用户
视图 \u form.html.erb

<%= simple_form_for (@list) do |f|  %>
 <%= f.error_notification  %>
 <div class= "form-inputs"> 
  <%= f.input :name %>
 </div>
 <h3>Items</h3>
 <table class= "table"> 
  <thead> 
   <tr> 
    <th></th>
    <th>Item Name</th>
    <th>Description</th>
  </tr>
 </thead>
 <tbody class="items"> 
  <%= simple_fields_for :items do |builder| %>
  <%= render 'item_fields', f: builder %>
  <%end %>
 </tbody>
 </table>

<div class= "form-actions"> 
 <%= f.button :submit %>
 <%= link_to_add_association 'Add Item', f, :items, class: 'btn btn- 
 primary', data: {association_insertion_node: '.items', 
 association_insertion_method: :append } %>
</div>
<% end %>
<tr class= "nested-fields"> 
 <td> 
  <%= link_to_remove_association "remove item", f, class: 'btn btn- 
  primary btn-xs' %>
 </td>
 <td> <%= f.input :name, label: false %></td>
 <td> <%= f.input :description, label: false %></td>
</tr>

项目
项目名称
描述
\u item\u fields.html.erb

<%= simple_form_for (@list) do |f|  %>
 <%= f.error_notification  %>
 <div class= "form-inputs"> 
  <%= f.input :name %>
 </div>
 <h3>Items</h3>
 <table class= "table"> 
  <thead> 
   <tr> 
    <th></th>
    <th>Item Name</th>
    <th>Description</th>
  </tr>
 </thead>
 <tbody class="items"> 
  <%= simple_fields_for :items do |builder| %>
  <%= render 'item_fields', f: builder %>
  <%end %>
 </tbody>
 </table>

<div class= "form-actions"> 
 <%= f.button :submit %>
 <%= link_to_add_association 'Add Item', f, :items, class: 'btn btn- 
 primary', data: {association_insertion_node: '.items', 
 association_insertion_method: :append } %>
</div>
<% end %>
<tr class= "nested-fields"> 
 <td> 
  <%= link_to_remove_association "remove item", f, class: 'btn btn- 
  primary btn-xs' %>
 </td>
 <td> <%= f.input :name, label: false %></td>
 <td> <%= f.input :description, label: false %></td>
</tr>

您忘了参考父模型。 改变这个

由此

问题出在application.js文件中, cocoon必须在jQuery之后加载

/=需要jquery3
//=需要rails ujs
//需要茧

在添加
cococoon
gem后,您是否重新启动了Rails应用程序?您的意思是关闭服务器并重新启动?正确,我查看了gem,但他们似乎没有对此进行任何更改。也许还可以试着运行
spring stop
谢谢,我会的!还有一个问题:您能否详细说明_todolist.json.jbuilder提供什么功能?我在源代码中看到了这些文件,但在cocoon的视频教程中没有介绍它们。非常感谢,这帮了我很大的忙!但由于某些原因,表单现在不会呈现“item partial”中的输入字段。有什么想法吗@Kobaltz请确保在新操作列表控制器#列表控制器
def new@list=list.new items:[item.new]end
中构建一个item对象,这又帮了大忙:谢谢!但现在我只有一个项目行。当我点击“添加项目”(见上面的代码)或“删除项目”时,它不起作用。有什么想法吗?嗨,路易斯·席尔瓦,谢谢你,你帮了我大忙!不过我还有一个问题。我现在可以添加一个项目,但是第二个项目行不会自动显示。我有点迷惑不解,因为我已经按照教程中的内容写了信。你介意看一下吗?嗨,伊曼纽尔,我找不到问题所在,但这里有一件事你应该考虑。您的部分必须命名为
\u item\u fields.html.erb
,因为在您的第二篇文章中,我将其视为
\u item.html.erb
,如果您愿意,还可以将项目上载到github或bitbucket,我可以尝试找出问题所在。