Ruby on rails Rails4-未定义的方法'name';零级:零级4

Ruby on rails Rails4-未定义的方法'name';零级:零级4,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我在show.html.erb页面中收到一条错误消息,上面显示未定义的方法“name”。当它说@book.category.name时,它一直在说未定义的方法名 show.html.erb <h1><%= @book.title %></h1> <h3><%= @book.author %></h3> <h4>Category: <%= @book.category.name %></

我在show.html.erb页面中收到一条错误消息,上面显示未定义的方法“name”。当它说@book.category.name时,它一直在说未定义的方法名


show.html.erb

    <h1><%= @book.title %></h1>
<h3><%= @book.author %></h3>
<h4>Category: <%= @book.category.name %></h4>
<p><%= @book.description %></p>

<%= link_to "Back", root_path %>


<% if user_signed_in? %>
<% if @book.user_id == current_user.id %>

<%= link_to "edit", edit_book_path(@book) %>
<%= link_to "Delete", book_path(@book), method: :delete, data: {confirm: "Are you sure you want to delete book?"} %>

<% end %>
<% end %>

类别:


Books_Controller.rb

    class BooksController < ApplicationController
before_action :find_book, only: [:show, :edit, :destroy, :update]

def index
if params[:category].blank?
@books = Book.all.order("created_at DESC")
else 
    @category_id = Category.find_by(name: params[:category]).id
    @books = Book.where(:category_id => @category_id).order("created_at DESC")
end
end

def show 
end

def new
@book = current_user.books.build
@categories = Category.all.map{ |c| [c.name, c.id]}
end

 def create 
@book = current_user.books.build(book_params)
@book.category_id = params[:category_id]


if @book.save
    redirect_to root_path
else
    render 'new'
    end
end

def edit
@categories = Category.all.map{ |c| [c.name, c.id]}
end

def update
 @book.category_id = params[:category_id]
if @book.update(book_params)
    redirect_to book_path(@book)
else
    render ' new'
end
end

def destroy
    @book.destroy
    redirect_to root_path
end

 private

 def book_params
params.require(:book).permit(:title, :description, :author, :category_id, :book_img)
 end

def find_book
@book = Book.find(params[:id])
end


end
class BooksController@category\u id).order(“在DESC创建”)
结束
结束
def秀
结束
def新
@book=当前_user.books.build
@categories=Category.all.map{| c |[c.name,c.id]}
结束
def创建
@book=当前用户.books.build(book参数)
@book.category\u id=参数[:category\u id]
如果@book.save
将\重定向到根\路径
其他的
呈现“新”
结束
结束
定义编辑
@categories=Category.all.map{| c |[c.name,c.id]}
结束
def更新
@book.category\u id=参数[:category\u id]
如果@book.update(book_参数)
将_重定向到book_路径(@book)
其他的
呈现“新”
结束
结束
def销毁
@毁灭
将\重定向到根\路径
结束
私有的
def book_参数
参数require(:book).permit(:title,:description,:author,:category\u id,:book\u img)
结束
def查找书
@book=book.find(参数[:id])
结束
结束

Book.rb

class Book < ActiveRecord::Base
    belongs_to :user
    belongs_to :category

    has_attached_file :book_img, :styles => { :book_index => "250x350>", :book_show => "325x475>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :book_img, :content_type => /\Aimage\/.*\Z/
end
classbook{:book\u index=>“250x350>”,:book\u show=>“325x475>”},:default\u url=>“/images/:style/missing.png”
验证\u附件\u内容\u类型:book\u img,:content\u type=>/\Aimage\/.\Z/
结束

Category.rb

class Category < ActiveRecord::Base
    has_many :books
end
类别
只需确保特定书籍有
类别
,或者只需将其包含在if语句中,如下所示:

<% if @book.category %>
<h4>Category: <%= @book.category.name %></h4>
<% end %>

类别:

只需确保特定书籍有
类别
,或者只需将其包含在if语句中,如下所示:

<% if @book.category %>
<h4>Category: <%= @book.category.name %></h4>
<% end %>

类别:

您需要确保实例
@book
具有
类别
对象

<h4>Category: <%= @book.category ? @book.category.name : "This book has no category" %></h4>
类别:

您需要确保实例
@book
具有
类别
对象

<h4>Category: <%= @book.category ? @book.category.name : "This book has no category" %></h4>
类别:

如果必须将类别留空,请使用:

类别:

如果必须将类别留空,请使用:

类别:

这意味着
@book
实例没有类别它意味着
@book
实例没有类别