【发布时间】:2014-05-12 20:47:46
【问题描述】:
我正在尝试关注this tutorial。它写在以前版本的 Rails 中,我使用的是 Rails 4。有一个下拉列表选择不出现,我收到以下错误:
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'} %>
这里是 migrate/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
这里是 migrate/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
我该怎么办?,提前谢谢你
【问题讨论】:
-
@subject 在您看来很可能是未定义的。你的控制器是什么样的?也尝试重新启动您的服务器(错误和视图似乎不同意有问题的行的内容)
标签: ruby-on-rails ruby-on-rails-4