0
我環顧四周,無法找到解決此特定問題的帖子。Rails 4更新book_reviews問題
在我的瀏覽器中,我進入索引(book_path)頁面查看已發佈的所有書籍。標題是和顯示頁面的鏈接,以及該書的詳細信息。
書籍/ index.html.erb
<% @books.each do |book| %><hr>
Title: <%= link_to "#{book.title}", book_path(book) %></a><br>
Description: <%= book.description %><br>
URL: <%= book.url %><br>
Role: <%= book.user.role %><br>
<%= link_to "Edit Book", edit_book_path(book) %>
<%= link_to 'Delete Book', book, method: :delete,
data: { confirm: 'Are you sure?' } %> <hr>
<% end %>
書籍/ show.html.erb - 因爲你可以看到我有一個從形式加入後的細節編輯的link_to。
<!-- show each book's detail -->
<div>
Title: <%= @book.title %><br>
Description: <%= @book.description %><br>
URL: <%= @book.url %><br>
<%= link_to "Edit Book", edit_book_path(@book) %>
</div><hr>
<!-- show each review -->
<div>
<% @book.reviews.each do |review| %>
<%= link_to "Edit", edit_book_review_path(@book, review) %>
Review: <%= review.description %><hr>
<% end %>
</div>
<%= render "reviews/reviewform" %>
評論/ edit.html.erb
<%= form_for :review, :url => { :action => 'update', @review.id => :review_id }, method: :patch do |f| %>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit "Update Review"%>
</p>
<% end %>
這是URL,當我在我的索引頁的書點擊我得到(預期)
http://localhost:3000/books/6
這是我點擊編輯評論(預計)後得到的網址
http://localhost:3000/books/6/reviews/13/edit
雖然當我點擊 「更新評論」 它給出了一個錯誤(意外)
ActiveRecord::RecordNotFound in BooksController#show
Couldn't find Book with 'id'=13
def show
@book = Book.find(params[:id])
end
其指我回到我的書控制器
reviews_controller.rb
def edit
@book = Book.find(params[:book_id])
@review = Review.find(params[:id])
end
def update
@review = Review.find(params[:id])
if @review.update_attributes(review_params)
redirect_to book_path, :notice => "Successfully Updated"
else
render "edit"
end
end
可理解因爲我將它重定向到book_path,但不應該只是向我展示帶有id 6和更新的評論的書籍?
,所以我把它改成
def update
@review = Review.find(params[:id])
if @review.update_attributes(review_params)
redirect_to book_review_path, :notice => "Successfully Updated"
else
render "edit"
end
end
,並得到
ActiveRecord::RecordNotFound in ReviewsController#show
Couldn't find Review with 'id'=6
def show
@review = Review.find(params[:book_id])
end
Request
Parameters:
{"book_id"=>"6",
"id"=>"13"}
的routes.rb
root GET / books#index
book_reviews GET /books/:book_id/reviews(.:format) reviews#index
POST /books/:book_id/reviews(.:format) reviews#create
new_book_review GET /books/:book_id/reviews/new(.:format) reviews#new
edit_book_review GET /books/:book_id/reviews/:id/edit(.:format) reviews#edit
book_review GET /books/:book_id/reviews/:id(.:format) reviews#show
PATCH /books/:book_id/reviews/:id(.:format) reviews#update
PUT /books/:book_id/reviews/:id(.:format) reviews#update
DELETE /books/:book_id/reviews/:id(.:format) reviews#destroy
books GET /books(.:format) books#index
POST /books(.:format) books#create
new_book GET /books/new(.:format) books#new
edit_book GET /books/:id/edit(.:format) books#edit
book GET /books/:id(.:format) books#show
PATCH /books/:id(.:format) books#update
PUT /books/:id(.:format) books#update
DELETE /books/:id(.:format) books#destroy
我曾嘗試其他途徑,但沒有運氣。 讓我知道如果你需要任何東西
它的工作..哈哈..這是令人興奮的。謝謝@Rustam – Diego 2014-12-27 17:50:24