【问题标题】:OnClick getting undefined method `model_name' for NilClass: ClassOnClick 获取 NilClass 的未定义方法“model_name”:类
【发布时间】:2016-01-07 11:15:51
【问题描述】:

我是 Rails 新手。我正在尝试创建一个包含姓名、电子邮件、电话号码和消息字段的表单。当我单击链接到表单的按钮时,出现错误:

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

这是 _form.html.erb 页面的代码:

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

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

<div class="col-md-4">
<div class="form-group">
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, :class=>"form-control", :placeholder=>"Enter Your Name" %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email, :class=>"form-control", :placeholder=>"Enter Your Email"%>
  </div>
  <div class="field">
    <%= f.label :phone %><br />
    <%= f.text_field :phone, :class=>"form-control", :placeholder=>"Enter Your Phone Number"%>
   </div>
</div> 
</div>
<div class = "col-md-8">
<div class="form-group">
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description,  :class=>"form-control", :size=>"20x5", :placeholder=>"Enter Your Message"%>
  </div>
</div>
</div>

 <div class="actions">
  <%= f.submit "Submit", :class=> "button1"%>
 </div>  
<% end %>

这是contacts_controller.rb的代码:

class ContactsController < ApplicationController


  # GET /contacts/new
  # GET /contacts/new.xml

  def new
  @contact = Contact.new

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @contact }
  end
end

# POST /contacts
# POST /contacts.xml
def create
  @contact = Contact.new(params[:contact])

  respond_to do |format|
    if @contact.save
      format.html { redirect_to(root_path, :notice => 'Thank you for contacting us. We will get back to you shortly.') }
      format.xml  { render :xml => @contact, :status => :created, :location => @contact }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @contact.errors, :status => :unprocessable_entity }
      end
    end
  end
end

另外,我在routes.rb 文件中添加了resources :contacts

请帮助我了解这里的问题。 谢谢。

【问题讨论】:

  • 你能把错误堆栈复制到这里吗?
  • 希望你也定义了一个Model contact.rb,只是确认一下?
  • 是的,我也定义了一个模型contact.rb
  • here's 完整错误。 @HarryBomrah
  • 这是模型class Contact &lt; ActiveRecord::Base validates_presence_of :name,:email end

标签: html ruby-on-rails erb


【解决方案1】:

请将routes.rb修改为:

使用

resources :contacts

代替

resources: contacts

【讨论】:

  • 谢谢,我只有在代码上是这样的。只是我这边的打字错误。
【解决方案2】:

您在部分调用@contact - 它应该是一个本地变量,在您调用部分时定义:

<%= render partial: "form", locals: { contact: @contact } %>

...然后...

#_form.html.erb
<%= form_for contact do ... 

model_name 错误来自 form_for - 当它接受一个对象时,它将寻找 model_name 属性,以填充路径等。

因为您收到与NilClass:Class 一起出现的错误,这表明您的@contact 变量未定义。

以上是我的建议;你不应该在你的部分调用@instance_variables

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多