简体   繁体   中英

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:

NoMethodError in Forms#show

undefined method `model_name' for nil:NilClass

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:

<%= 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:

class customformd < ApplicationRecord
  belongs_to :form
end

Here's my customformds_controller code:

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:

<%= render @form.customformd %>
<h5>Add Custom Form D Specific Info:</h5>
<%= render 'customformds/form' %>

Thank you very much in advance!

NoMethodError in Forms#show

undefined method `model_name' for nil:NilClass

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_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 )

class Customformd < ApplicationRecord
  belongs_to :form
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM