【发布时间】:2017-08-17 14:57:56
【问题描述】:
我真的是 RoR 的新手,我正在尝试使用 simple_form 创建一个嵌套表单,但我一直看到这个错误出现:
表单中的 NoMethodError#show
nil:NilClass 的未定义方法 `model_name'
我查看了这些链接,但无济于事。我也考虑过我是否有任何拼写错误,或者在使用单数/复数时犯了错误。
- Creating multiple nested forms using simple_form and rails 4
- How should I use rails and simple_form for nested resources?
- Rails - Using form_for and fields_for, how do you access the sub-object while in the fields_for block?
- Ruby on Rails: undefined method `model_name' for NilClass:Class
这是我的_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 %>
这是我的customformd.rb 代码:
class customformd < ApplicationRecord
belongs_to :form
end
这是我的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
这是show.html.erb 代码中违规部分的 sn-p:
<%= render @form.customformd %>
<h5>Add Custom Form D Specific Info:</h5>
<%= render 'customformds/form' %>
非常感谢您!
【问题讨论】:
标签: ruby-on-rails ruby forms