I am getting the following error I am new to ruby and rails
I am using Rails4 with eclipse plugin the error is
NoMethodError in Books#show
undefined method `name' for nil:NilClass
Extracted source (around line #3):
@book.subject.name I used associations in the model book which is belongs_to association with subject model.
<h1><%= @book.title %></h1>
<p><strong>Price: </strong> $<%= @book.price %><br />
<strong>Subject: </strong> <%= link_to @book.subject.name, #line number 3
:action => "show_subjects", :id => @book.subject.id %><br />
<strong>Created Date:</strong> <%= @book.created_at %><br />
</p>
app/views/books/show.erb:3:in `_app_views_books_show_erb___1047429498_14766720'
actionpack (4.0.3) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.3) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.3) lib/action_view/template.rb:141:in `render'
routes.rb file is
LibrarayWebApplication::Application.routes.draw do
get 'books/list'
get 'books/new'
post 'books/create'
post 'books/update'
get 'books/list'
get 'books/show'
get 'books/show_subjects'
get 'books/edit'
get 'books/delete'
get 'books/update'
get 'books/show_subjects'
end
my migrations files are
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
t.column :created_at, :timestamp
end
end
def self.down
drop_table :books
end
end
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
my controller files is
class BooksController < ApplicationController
def list
@books = Book.find(:all)
end
def show
@book = Book.find(params[:id])
end
def new
@book = Book.new
@subjects = Subject.find(:all)
end
# private
def book_params
params.require(:book).permit(:title,:price,:subject,:description)
end
def create
@book = Book.new(book_params)
if @book.save
redirect_to :action => 'list'
else
@subjects = Subject.find(:all)
render :action => 'new'
end
end
def edit
@book = Book.find(params[:id])
@subjects = Subject.find(:all)
end
def update
@book = Book.find(params[:id])
if @book.update_attributes(book_params)
redirect_to :action => 'show', :id => @book
else
@subjects = Subject.find(:all)
render :action => 'edit'
end
end
def delete
Book.find(params[:id]).destroy
redirect_to :action => 'list'
end
def show_subjects
@subject = Subject.find(params[:id])
end
end
show.erb file is
I used associations in the model book which is belongs_to association with subject model.
<h1><%= @book.title %></h1>
<p><strong>Price: </strong> $<%= @book.price %><br />
<strong>Subject: </strong> <%= link_to @book.subject.name, #this line I am getting error
:action => "show_subjects", :id => @book.subject.id %><br />
<strong>Created Date:</strong> <%= @book.created_at %><br />
</p>
<p><%= @book.description %></p>
<hr />
<%= link_to 'Back', {:action => 'list'} %>
show_subjects file is
<h1><%= @subject.name -%></h1>
<ul>
<% @subject.books.each do |c| %>
<li><%= link_to c.title, :action => "show", :id => c.id -%></li>
<% end %>
</ul>
I have two models
book.erb
class Book < ActiveRecord::Base
belongs_to :subject
validates_presence_of :title
validates_numericality_of :price, :message=>"Error Message"
end
I made associations between the models in book model I used belongs_to and in subject model I used has_many but I am getting that error If I remove the subject in show.erb the application working fine but when I include that line it showing that no such method
我在书籍模型中使用了belongs_to的模型之间建立了关联,在主题模型中我使用了has_many,但是我收到了错误如果我在show.erb中删除主题,应用程序工作正常,但当我包含该行时,它显示了没有这样的方法 我在书模型中使用belongs_to和模型之间建立了关联我使用has_many但我得到了错误如果我在show.erb中删除主题应用程序工作正常但是当我包含该行时它显示没有这样的方法
subject.erb
class Subject < ActiveRecord::Base
has_many :books
end
我写了@ book.subject.name如果@ book.subject这句话避免了错误,但它无法从数据库中检索主题名称
答案 0 :(得分:0)
您收到的是未定义的方法错误,因为您没有该特定图书的主题。换句话说,您应该在实际调用book.subject.name之前检查是否存在图书主题。
P.S。我会改变您的主题迁移(抛弃Subject.create&#39; ...&#39;部分)并使用资源:路线的书籍,而不是单独定义所有路线。
答案 1 :(得分:0)
在show.html.erb而不是@book.subject.name
中写为@book.subject.try(:name)
或@book.subject.name if @book.subject