[英]Ruby on rails scaffold error in _form.html.erb
This CURD generated by scaffold method using following command 使用以下命令通过脚手架方法生成此CURD
rails g scaffold contact fullname:string surname:string email:string mobile:string note:text
when i go to localhost:3003/contacts/new getting following error 当我转到localhost:3003 / contacts / new时出现以下错误
NoMethodError in Contacts#new
Showing /root/contactlist/app/views/contacts/_form.html.erb where line #16 raised:
undefined method `fullname' for #<Contact:0xb2fa75b4>
Extracted source (around line #16):
13:
14: <div class="field">
15: <%= f.label :fullname %><br />
16: <%= f.text_field :fullname %>
17: </div>
18: <div class="field">
19: <%= f.label :surname %><br />
Trace of template inclusion: app/views/contacts/new.html.erb
Rails.root: /root/contactlist
Application Trace | Framework Trace | Full Trace
app/views/contacts/_form.html.erb:16:in `_app_views_contacts__form_html_erb___387033993__645411558'
app/views/contacts/_form.html.erb:1:in `_app_views_contacts__form_html_erb___387033993__645411558'
app/views/contacts/new.html.erb:3:in `_app_views_contacts_new_html_erb__1061805842__644918458'
app/controllers/contacts_controller.rb:29:in `new'
This is my views/contacts/_form.html.erb 这是我的views / contacts / _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="field">
<%= f.label :fullname %><br />
<%= f.text_field :fullname %>
</div>
<div class="field">
<%= f.label :surname %><br />
<%= f.text_field :surname %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :mobile %><br />
<%= f.text_field :mobile %>
</div>
<div class="field">
<%= f.label :note %><br />
<%= f.text_area :note %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
this is the Controller file "contacts_controller.rb" 这是控制器文件“ contacts_controller.rb”
class ContactsController < ApplicationController
# GET /contacts
# GET /contacts.json
def index
@contacts = Contact.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @contacts }
end
end
# GET /contacts/1
# GET /contacts/1.json
def show
@contact = Contact.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @contact }
end
end
# GET /contacts/new
# GET /contacts/new.json
def new
@contact = Contact.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @contact }
end
end
# GET /contacts/1/edit
def edit
@contact = Contact.find(params[:id])
end
# POST /contacts
# POST /contacts.json
def create
@contact = Contact.new(params[:contact])
respond_to do |format|
if @contact.save
format.html { redirect_to @contact, :notice => 'Contact was successfully created.' }
format.json { render :json => @contact, :status => :created, :location => @contact }
else
format.html { render :action => "new" }
format.json { render :json => @contact.errors, :status => :unprocessable_entity }
end
end
end
# PUT /contacts/1
# PUT /contacts/1.json
def update
@contact = Contact.find(params[:id])
respond_to do |format|
if @contact.update_attributes(params[:contact])
format.html { redirect_to @contact, :notice => 'Contact was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @contact.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /contacts/1
# DELETE /contacts/1.json
def destroy
@contact = Contact.find(params[:id])
@contact.destroy
respond_to do |format|
format.html { redirect_to contacts_url }
format.json { head :no_content }
end
end
end
This is the Model file "contact.rb" 这是模型文件“ contact.rb”
class Contact < ActiveRecord::Base
attr_accessible :email, :fullname, :mobile, :note, :surname
end
Please help me solve this error i'm stuck. 请帮助我解决我遇到的这个错误。
Everything looks right. 一切看起来都不错。 Run
rake db:migrate
then restart the server. 运行
rake db:migrate
然后重新启动服务器。
Edit: 编辑:
if you have created the table already and you want to recreate it then drop the old table first 如果您已经创建了表并且想要重新创建它,则先删除旧表
Add this to the top of the migration 将此添加到迁移的顶部
def change
drop_table contacts
.... #create the new contact table
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.