rails form_for另一个动作

时间:2014-01-26 15:27:59

标签: ruby-on-rails form-for

我有一本书类,希望用户更新其排名。 books_controller中方法的代码是:

def update_book_rank
  @book = Book.find(params[:id])
  @book.rank = @book.rank + params[:rank]
  if @book.update!(book_params)
    redirect_to view_books_books_path
  else
    render 'edit'
  end
end

和相应的html.erb:

<h1>Adding rank to a book</h1>
<p>Name of the book: <%= @book.title %></p>
<p>Author of the book: <%= @book.author.name %></p>
<p>Please rank the book from 0(worst rating) to 10(best rating):</p>
<%= form_for @book, :url => update_book_rank_books_path, :method => :patch do |f| %>
  <p>
    <%= f.label :rank %><br>
    <%= f.text_field :rank %>
  </p>

  <%= f.submit "Enter rank" %>
<% end %>

发生以下错误:

ActiveRecord::RecordNotFound in BooksController#update
Couldn't find Book with id=update_book_rank 
Extracted source (around line #25):
24 def update
25 @book = Book.find(params[:id])
26 authorize! :read, @book
27 if @book.update!(book_params)
28 redirect_to @book

Parameters:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"kRt2RWNlKyBqMnEvoloLyJLwWrhjM5k1n9sw93Ar7eY=",
 "book"=>{"rank"=>"10"},
 "commit"=>"Enter rank",
 "id"=>"update_book_rank"}

0 个答案:

没有答案