nil的未定义方法'id':NilClass - 编辑路由不起作用

时间:2017-06-30 03:33:52

标签: ruby-on-rails activerecord sinatra

enter image description here

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 "/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>

2 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您尝试访问/books/edit而不是/books/1/edit。检查您要创建的链接以编辑图书。

答案 1 :(得分: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,请更改它。