我是ruby on rails的新手,我目前无法尝试创建联系表单。 我得到报告说:#的未定义方法`name',但是我确实为name创建了方法: create_table:contacts do | t | t.string:name 我试图修复此错误,以便我可以预览页面,但我一直收到错误。我希望你们中的一位能够帮助我,提前谢谢!
NoMethodError in Contacts#new
Showing /home/nitrous/workspace/simplecodecast_saas/app/views/contacts/new.html.erb where line #7 raised:
undefined method `name' for #<Contact id: nil>
Extracted source (around line #7):
<%= form_for @contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %> <---- (This is line 7)
</div>
<div class="form-group">
Rails.root: /home/nitrous/workspace/simplecodecast_saas
Application Trace | Framework Trace | Full Trace
app/views/contacts/new.html.erb:7:in `block in _app_views_contacts_new_html_erb__2291340040590759835_34535240'
app/views/contacts/new.html.erb:4:in `_app_views_contacts_new_html_erb__2291340040590759835_34535240'
Request
Parameters:
None
Toggle session dump
Toggle env dump
Response
Headers:
None
我的routes.rb代码:
Rails.application.routes.draw do
resources :contacts
get '/about' => 'pages#about'
root 'pages#home'
和我的contacts_controller.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
end
end
我将此添加到我的模型中:contact.rb
class Contact < ActiveRecord::Base
end
db文件:
class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.text :comments
t.timestamps
end
end
end
并持续我的HTML页面
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well">
<%= form_for @contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<% f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
答案 0 :(得分:0)
我找到了解决办法。 我运行了rake db:rollback cmd,然后是rake db:migrate 现在它正在发挥作用。
抱歉浪费你的时间:)希望其他人可以从这个答案中受益!