首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoMethodError in Books#show

NoMethodError in Books#show
EN

Stack Overflow用户
提问于 2014-03-19 07:02:48
回答 2查看 683关注 0票数 0
代码语言:javascript
复制
    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中的主题,应用程序运行得很好,那么我就会得到这个错误,但是当我包括该行时,它显示没有这样的方法。

代码语言:javascript
复制
    subject.erb
    class Subject < ActiveRecord::Base
        has_many :books
    end

我编写了@book.subject.name,如果这个语句避免了错误,但是它无法从数据库中检索主题名的话,@book.subject

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-19 07:34:34

您正在得到未定义的方法错误,因为您没有关于该特定书籍的主题。换句话说,在实际调用book.subject.name之前,您应该检查是否存在图书主题。

附注:我会改变你的主题迁移(抛弃Subject.create‘.’)并使用资源:为您的路线而不是单独定义所有路线的书籍。

票数 0
EN

Stack Overflow用户

发布于 2014-03-19 10:30:28

在show.html.erb中,而不是@book.subject.name中,写为@book.subject.try(:name)@book.subject.name if @book.subject

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22498621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档