Ron*_*n M 4 ruby-on-rails cocoon-gem
我正在尝试在Rails 4应用程序中使用嵌套资源构建表单。我正在用茧宝石。每个步骤都有子步骤,我希望允许用户向表单添加尽可能多的子步骤,他/她希望。
Step.rb
class Step < ActiveRecord::Base
has_many :substeps
accepts_nested_attributes_for :substeps
Run Code Online (Sandbox Code Playgroud)
Substep.rb
class Substep < ActiveRecord::Base
belongs_to :step
Run Code Online (Sandbox Code Playgroud)
表格代码
<%= form_for :step, :url => steps_path do |f| %>
<%= text_field(:step, :title, :value => '', class: 'fly-input input_info', placeholder: 'Process Title', id: 'step_form_title') %>
<%= text_field(:step, :description, :value => '', class: 'fly-input input_info', placeholder: 'Process Description', id: 'step_form_description') %>
<%= hidden_field :step, :known %>
<%= hidden_field_tag :experiment, @experiment.id %>
<%= f.fields_for :substep do |ff| %>
<%= ff.text_field :description %>
<% end %>
<%= link_to_add_association 'Add substep', f, :substeps %>
<%= f.submit "Done", class: "main_button" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到一条错误消息:此行上的“ NilClass:Class的未定义方法'reflect_on_association'”
<%= link_to_add_association 'Add substep', f, :substeps %>
Run Code Online (Sandbox Code Playgroud)
对我的问题有什么想法吗?
编辑 根据Pavan的建议,将text_field更改为ff.text_field
Cocoon希望像您一样提供表单对象作为第二个参数,但也希望该第二个参数实际上将Rails模型实例作为attribute f.object。
您的表单仅使用模型名称创建,form_for :step因此Cocoon引发异常。
为了解决这个问题,你应该将其更改为form_for @step在那里@step可以Step.new和任何其他Step实例。