[英]Ruby Rails - NoMethodError , drop-down list
我正在尝试按照本教程进行操作 。 它是在先前版本的Rails中编写的,而我正在使用Rails4。没有一个下拉列表,但该选择没有出现,我收到以下错误消息:
NoMethodError in Book#show
Showing C:/Ruby193/mylibrary/app/views/book/show.html where line #3 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #3):
1 <h1><%= @book.title %></h1>
2 <p><strong>Price: </strong> $<%= @book.price %><br />
3 <strong>Subject: </strong> <%= @subject.name %><br />
4 </p>
5 <p><%= @book.description %></p>
6 <hr />
Rails.root: C:/Ruby193/mylibrary
这是show.html
<h1><%= @book.title %></h1>
<p><strong>Price: </strong> $<%= @book.price %><br />
<strong>Subject: </strong> <%= @book.subject.name %><br />
</p>
<p><%= @book.description %></p>
<hr />
<%= link_to 'Back', {:action => 'list'} %>
这是migration / 258412_subjects.rb
class Subjects < ActiveRecord::Migration
def self.up
create_table :subjects do |t|
t.column :name, :string
end
Subject.create :name => "Physics"
Subject.create :name => "Mathematics"
Subject.create :name => "Chemistry"
Subject.create :name => "Psychology"
Subject.create :name => "Geography"
end
def self.down
drop_table :subjects
end
end
这是migration / 05465_books.rb
class Books < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.column :title, :string, :limit => 32, :null => false
t.column :price, :float
t.column :subject_id, :integer
t.column :description, :text
end
end
def self.down
drop_table :books
end
end
这是models / book.rb
class Book < ActiveRecord::Base
belongs_to :subject
validates_presence_of :title
validates_numericality_of :price, :message=>"Error Message"
end
这是我的控制器类book_controller.rb
class BookController < ApplicationController
def list
@books = Book.all
end
def show
@book = Book.find(params[:id])
end
def new
@book = Book.new
@subjects = Subject.all
end
def create
@book = Book.new(book_params)
if @book.save!
redirect_to :action => 'list'
else
@subjects = Subject.all
render :action => 'new'
end
end
def edit
@book = Book.find(params[:id])
@subjects = Subject.all
end
def update
@book = Book.find(params[:id])
if @book.update_attributes(book_params)
redirect_to :action => 'show', :id => @book
else
@subjects = Subject.all
render :action => 'edit'
end
end
def delete
Book.find(params[:id]).destroy
redirect_to :action => 'list'
end
private
def book_params
params.permit(:title, :price, :description)
end
end
我该怎么办?,提前谢谢
根据你的错误
nil:NilClass的未定义方法“名称”
Extracted source (around line #3):
1 <h1><%= @book.title %></h1>
2 <p><strong>Price: </strong> $<%= @book.price %><br />
3 <strong>Subject: </strong> <%= @subject.name %><br />
@subject在您的表演中为零。 由于您的控制器有
def show
@book = Book.find(params[:id])
end
对于您的节目,似乎是这种情况。
您为演出发布的代码的行为
<strong>Subject: </strong> <%= @book.subject.name %><br />
我建议您尝试重新启动服务器,因为它似乎已缓存了较旧版本的视图。
你应该写
@book.subject.name
当然,您应该测试是否设置了subject
。 一种方法是写
@book.subject.try(:name)
如果subject
不为零,它将尝试name
。
还有一个一般性的评论:不要从迁移内部为数据库添加种子。 通常,您只迁移一次,但是在进行生产或测试时,您将运行rake db:setup
,它将创建数据库架构而不运行迁移。
而是使用db/seed.rb
播种数据库。 这是一个可重复的过程,运行测试时也将需要此过程(从干净/空数据库开始)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.