【发布时间】: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 < ActiveRecord::Base validates_presence_of :name,:email end
标签: html ruby-on-rails erb