2017-06-30 13 views
-1

enter image description here未定義のメソッド「ID」:NilClass - 編集ルートは

enter image description here

を動作しません私は、なぜ私はエラーを取得しています本当にわかりません。これ以外のルートはすべて動作します。

は、私はまた、

class BooksController < ApplicationController 
    get '/books' do 
    if logged_in? 
     @books = Book.all 
     erb :'books/index' 
    else 
     redirect to '/login' 
    end 
    end 

    get '/books/new' do 
    if logged_in? 
     erb :'books/new' 
    else 
     redirect to '/login' 
    end 
    end 

    post '/books' do 
    @book = Book.create(:title => params[:title], :author => params[:author]) 
    if @book.save 
     redirect to "/books/#{@book.id}" 
    else 
     redirect to '/books/new' 
    end 
    end 

    get '/books/:id' do 
    if logged_in? 
     @book = Book.find_by_id(params[:id]) 
     erb :'books/show' 
    else 
     flash[:message] = "Please login to access your library." 
     redirect to '/login' 
    end 
    end 

    get '/books/:id/edit' do 
    if logged_in? 
     @book = Book.find_by_id(params[:id]) 
     if @book.user_id == current_user.id 
     erb :'books/edit' 
     else 
     redirect to '/books' 
     end 
    else 
     redirect to '/login' 
    end 
    end 

    patch '/books/:id' do 
    if params[:title] == "" || params[:author] == "" 
     redirect to "/books/#{params[:id]}/edit" 
    else 
     @book = Book.find_by_id(params[:id]) 
     @book.title = params[:title] 
     @book.author = params[:author] 
     @book.save 
     redirect to "/book/#{@book.id}" 
    end 
    end 

    delete '/books/:id/delete' do 
    if logged_in? 
     @book = Book.find_by_id(params[:id]) 
     @book.user_id == current_user.id 
     @book.delete 
     redirect to '/books' 
    else 
     redirect to '/login' 
    end 
    end 

end 

がUserController

class UsersController < ApplicationController 
    get '/' do 
    erb :'index' 
    end 

    get '/users/:slug' do 
     @user = User.find_by_slug(params[:slug]) 
     erb :'users/show' 
    end 

    get '/signup' do 
    if !logged_in? 
     erb :'users/new' 
    else 
     redirect to '/books' 
    end 
    end 

    get '/login' do 
    if !logged_in? 
     erb :'users/login' 
    else 
     redirect to '/books' 
    end 
    end 

    post '/signup' do 
    @user = User.new(:username => params[:username], :email => params[:email], :password => params[:password]) 
    if params[:username].nil? || params[:email].nil? || params[:password].nil? 
     flash[:message] = "Please fill the form completely." 
     redirect to '/signup' 
    else 
     @user.save 
     session[:user_id] = @user.id 
     redirect to '/books' 
    end 
    end 

    post '/login' do 
    @user = User.find_by(:username => params[:username]) 
    if @user && @user.authenticate(params[:password]) 
     session[:user_id] = @user.id 
     redirect to "https://stackoverflow.com/users/#{@user.slug}" 
    else 
     flash[:message] = "Try again." 
     redirect to '/login' 
    end 
    end 

    get '/logout' do 
    if logged_in? 
     session.clear 
     redirect to '/login' 
    else 
     redirect to '/' 
    end 
    end 

end 

編集ビュー

BooksController

の下に私のコントローラとビューのコードを添付しています

<h1> Edit Your Book </h1> <form method="POST" action="/books/<%= @book.id %>"> <input id="hidden" type="hidden" name="_method" value="PATCH"> <input type="text" name="title" value="<%= @book.title %>"> <input type="text" name="author" value="<%= @book.author %>"> <input type="submit" value="Submit" id="submit"> </form> 
+0

私は、問題はそれがブック '@book =を見つけることができないと思いBook.find_by_id(params [:id]) 'これをログに記録し、それがヌルでないかどうかを調べるには、' puts "\ n \ nbook =" + @ book'で実行し、コンソールで確認してください。編集ビューでは 'form method =" POST "action ="/books/<%= @ book.id%> ">'を呼び出そうとし、 '@ book'がnilならnilクラスのメソッドid – Hatik

+0

@marsieあなたのコントローラで編集中に 'POST 'メソッドを渡す理由は、' patch'/books /:id '、その' PATCH'メソッドとして宣言されています。 –

答えて

0

これは、の代わりに/books/editにアクセスしようとしているために発生しています。ブックの編集のために作成しているリンクを確認してください。

0

あなたのコントローラでは、あなたがpatch

patch '/books/:id' do 
    if params[:title] == "" || params[:author] == "" 
    redirect to "/books/#{params[:id]}/edit" 
    else 
    @book = Book.find_by_id(params[:id]) 
    @book.title = params[:title] 
    @book.author = params[:author] 
    @book.save 
    redirect to "/book/#{@book.id}" 
    end 
end 

としてあなたのルートを定義しているそして、あなたのフォームがPOSTを言い、それを変更..

関連する問題