2017-06-30 44 views
-1

enter image description here未定義的方法「身份證」的零:NilClass - 編輯路線行不通

enter image description here

我真的不知道爲什麼,我發現了錯誤。除此之外,其他每條路線都有效。

我還附上下面

爲我的控制器和視圖代碼BooksController中

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 

UsersController

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 

編輯視圖

<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'並在控制檯中檢查它。因爲在你的編輯視圖中,你試圖調用'

」>'如果'@ book'爲零,那麼nil類的方法ID – Hatik

+0

@marsie爲什麼你在編輯時傳遞'POST'方法,在你的控制器中,聲明爲'patch'/ books /:id',它的'PATCH'方法。 –

回答

0

發生這種情況是因爲您嘗試訪問/books/edit而不是/books/1/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,改變它..

相關問題