[英]Nested form in simple_form
I'm really new to RoR, and I'm trying to create a nested form using simple_form but I keep seeing this error turn up: 我对RoR真的很陌生,我正在尝试使用simple_form创建嵌套表单,但我一直看到出现此错误:
NoMethodError in Forms#show Forms#show中的NoMethodError
undefined method `model_name' for nil:NilClass nil:NilClass的未定义方法“ model_name”
I've taken a look at these links, but to no avail. 我看过这些链接,但无济于事。 I've also considered whether I have any typos, or have made a mistake about use of the singular/plural. 我还考虑过我是否有错别字,或者在使用单/复数时犯了一个错误。
Here's my _form.html.erb
code, the error seems to be coming from the first line: 这是我的_form.html.erb
代码,该错误似乎来自第一行:
<%= simple_form_for([@form, @customformd]) do |f| %>
<%= f.input :legislation, label: 'Which Act?' %>
<%= f.input :provision, label: 'Which provision?', collection: [ "Act A", "Act B", "Act C" ] %>
<%= f.input :RB, label: 'Referring Body', collection: [ "A", "B", "C", "D", "E", "F"] %>
<%= f.button :submit %>
<% end %>
Here's my customformd.rb
code: 这是我的customformd.rb
代码:
class customformd < ApplicationRecord
belongs_to :form
end
Here's my customformds_controller
code: 这是我的customformds_controller
代码:
class CustomformdsController < ApplicationController
def create
@form = Form.find(params[:form_id])
@cformftcd = @form.customformds.create(customformds_params)
redirect_to form_path(@form)
end
private
def customformd_params
params.require(:customformd).permit(:RB, :legislation, :provision)
end
end
Here's a snippet of the offending part of the show.html.erb
code: 这是show.html.erb
代码中令人反感的部分的代码片段:
<%= render @form.customformd %>
<h5>Add Custom Form D Specific Info:</h5>
<%= render 'customformds/form' %>
Thank you very much in advance! 提前非常感谢您!
NoMethodError in Forms#show Forms#show中的NoMethodError
undefined method `model_name' for nil:NilClass nil:NilClass的未定义方法“ model_name”
You are rendering the _form
partial in forms/show.html.erb
, so make sure you have defined @form
and @customformd
in forms#show
method like below 您呈现_form
部分中forms/show.html.erb
,所以一定要确保你已经定义@form
和@customformd
在forms#show
方法如下图所示
#form_controller.rb
def show
@form = Form.find(params[:id])
@customformd = Customformd.new
end
Also, the class names should start with capital letter (i.,e customformd
should be Customformd
) 此外, 类别名称应以大写字母开头 (即, customformd
应该为Customformd
)
class Customformd < ApplicationRecord
belongs_to :form
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.