simple_form_for NilClass的未定义方法`model_name':Class

nul*_*tek 1 ruby-on-rails simple-form

在构建基本表单以创建新数据时,我收到以下错误.当我点击提交时,我得到了

simple_form_for NilClass的未定义方法`model_name':Class

**_form.html.erb**
<%= simple_form_for(@calls) do |f| %>
  <%= f.error_notification %>
  <%= f.input :caller_name %>
  <%= f.input :caller_phone, hint: "xxx-xxx-xxxx" %>
  <%= f.input :caller_address %>
  <%= f.input :primary_diagnosis %>
  <%= f.error :base %>
  <%= f.button :submit %>
<% end %>

**calls_controller.rb**
def create
     @call = Call.new(params[:call])

     respond_to do |format|
       if @call.save
         format.html { redirect_to @call, notice: 'Call was successfully created.' }
         format.json { render json: @call, status: :created, location: @call }
       else
         format.html { render action: "new" }
         format.json { render json: @call.errors, status: :unprocessable_entity }
       end
     end
   end

**new.html.erb**
<h1>New Call</h1>

<%= render 'form' %>

<%= link_to 'Back', calls_path %>
Run Code Online (Sandbox Code Playgroud)

因为我遵循了rails命名约定,所以我在这里有点迷失,甚至尝试使用具有相同结果的脚手架.已经重新启动了webrick.救命?

小智 5

你要的是 simple_form_for(@calls)

然而你的控制器正在创造 @call

你应该改变

<%= simple_form_for(@calls) do |f| %>
Run Code Online (Sandbox Code Playgroud)

<%= simple_form_for(@call) do |f| %>
Run Code Online (Sandbox Code Playgroud)