2014-05-12 63 views
1

我正在嘗試關注this tutorial。它寫在以前版本的Rails的,我用Rails 4.有選擇不會出現一個下拉列表,我收到以下錯誤:Ruby Rails - NoMethodError,下拉列表

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'} %> 

這裏是遷移/ 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 

在這裏被遷移/ 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 

下面是模型/ 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 

我應該怎麼辦?謝謝你在前進

+1

@subject很可能在您的視圖中未定義。你的控制器是什麼樣的?也嘗試重新啓動你的服務器(錯誤和視圖似乎不同意所討論的內容) – user3334690

回答

0

你應該寫

@book.subject.name 

當然這時如果subject設置你應該測試。要做到這一點的方法之一是寫

@book.subject.try(:name) 

這將將嘗試name如果subject不爲零。

還有一般性說法:不要在遷移中對數據庫進行種子處理。通常您只會遷移一次,但是當您進入生產或測試時,您將運行rake db:setup,這將創建數據庫模式而不運行遷移。

而是使用db/seed.rb來爲數據庫播種。這是一個可重複的過程,當運行測試時(從一個乾淨/空的數據庫開始),這也是需要的。

+0

我甚至會爭辯說,主題應該是屬於Book類的常量或類方法 – user3334690

+0

非常感謝,這方式已經解決了我的問題,我不採取錯誤,但選擇仍然沒有出現 – Mukaddes

0

根據到你的錯誤

未定義的方法`name'爲零:NilCl屁股

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 /> 

我會建議你嘗試重新啓動您的服務器,因爲它似乎已緩存的視圖的舊版本。