簡體   English   中英

NoMethodError未定義的方法,用於nil:NilClass

[英]NoMethodError undefined method for nil:NilClass

我正在運行Windows 10。

rails generate migration add_user_id_to_books user_id:integer
rake db:migrate

我想從當前用戶創建一本書。 但是,當我在導航欄上單擊“添加書”時,會收到以下消息:

NoMethodError in BooksController#new undefined method `books' for nil:NilClass

提取的源(圍繞第12行):10 11 12 13 14 15

def new
  @book = current_user.books.build
end

book.rb

class Book < ApplicationRecord
  belongs_to :user
end

user.rb

class User < ApplicationRecord
  has_many :books

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

books_controller.rb

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

   def index
    @books = Book.all.order("created_at DESC")
   end

  def show
  end   

  def new
    @book = current_user.books.build
  end

  def create
    @book = current_user.books.build(book_params)

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

  def edit
  end

  def update
    if @book.update(book_params)
      redirect_to book_path(@book)
    else
      render 'edit'
    end     
  end

  def destroy
    @book.destroy
    redirect_to root_path
  end   

  private

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

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

routes.rb

Rails.application.routes.draw do
devise_for :users
resources :books
root 'books#index'
end

application.html.erb

  <!DOCTYPE html>
  <html>
  <head>
 <title>BookReview</title>
 <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-        track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>

<nav class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header">
        <%= link_to "Book Review", root_path, class: "navbar-brand" %>
      </div>
      <ul class="nav navbar-nav">
        <li><%= link_to "Sign Up", new_user_registration_path %></li>
        <% if user_signed_in? %>
          <li><%= link_to "Sign Out", destroy_user_session_path, method: :delete %><li>
        <% else %>
          <li><%= link_to "Log In", new_user_session_path %></li>   

        <% end %>
      </ul>

      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Add Book", new_book_path %></li>
        <% if user_signed_in? %>
        <% end %>           
      </ul> 


    </div>

</nav>  

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

<%= yield %>

current_user在您的new操作中為nil 而且您不能在nil對象上調用books方法。

向您的控制器添加一個before_filter ,以確保current_user不為nil (即,一個用戶已登錄)。

class BooksController < ApplicationController
  before_action :authenticate_user!
  # rest of the code
end

authenticate_user! 是由devise gem提供的輔助方法。 當用戶未登錄時, authenticate_user! 方法會將用戶重定向到登錄頁面。

有關更多信息,請參見https://github.com/plataformatec/devise#controller-filters-and-helpers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM