简体   繁体   English

未定义的方法“ model_name”,Rails

[英]undefined method `model_name', Rails

I am trying to implement simple form in home#index page using: 我正在尝试使用以下方法在home#index页面中实现简单形式:

<%= render "forms/form"%>

Form look like this: 表单如下所示:

<%= form_for(@form) do |f| %>
  <% if @form.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@form.errors.count, "error") %> prohibited this form from being saved:</h2>

      <ul>
      <% @form.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :defect %><br />
    <%= f.text_field :defect %>
  </div>
  <div class="field">
    <%= f.label :region %><br />
    <%= f.text_field :region %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

But when I access that page, I got this error message: 但是,当我访问该页面时,出现以下错误消息:

undefined method `model_name' for NilClass:Class

First of all I though it is because my model name is also Form, but then I checked Rails 3 reserved words, but there wasn't "form" ! 首先,我虽然是因为我的模型名称也是Form,但随后我检查了Rails 3的保留字,但是没有“ form”!

Thanks in advance! 提前致谢!

Your @form instance variable's value is nil . 您的@form实例变量的值为nil You should set it in controller. 您应该在控制器中设置它。

In home controller, set the instance variable @form in the index action as:- 在家庭控制器中,将索引操作中的实例变量@form设置为:-

def index
  @form = Form.new
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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