【发布时间】:2015-11-08 19:34:43
【问题描述】:
我正在处理一个有效的编辑页面。但是,当我单击保存按钮以保存我所做的更改时,在 rails 4 中,我收到以下消息: No route matches [PATCH] "/book.17" 有关如何解决此问题的任何建议?我已经研究了一段时间,并认为这与我的路线有关,而不是指向正确的页面。只是不知道我应该如何改变它。我尝试使用 patch/put 而不是 get 进行编辑操作或将补丁“books#update”更新但得到相同的错误消息。任何帮助将不胜感激!这是代码:
控制器:
class BooksController < ApplicationController
def new
#@book = Book.all
@book = Book.new
@authors = Author.all
end
def edit
@book = Book.find_by_id(params[:id])
@authors = Author.all
end
def update
@book = Book.find_by_id(params[:id])
if @book.update_attributes(book_params)
flash[:success] = "Book Updated!"
redirect_to @book
else
render 'edit'
end
end
路线页面:
Rails.application.routes.draw do
root 'welcome#index'
get 'author' => 'authors#new'
get 'name' => 'authors#show'
get 'book' => 'books#new'
get 'show' => 'books#show'
patch 'edit' => 'books#update'
resources :authors
resources :books
编辑页面:
<div class="move">
<h1>Update a book entry</h2>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@book) do |f| %>
<%= render 'form' %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :pub_date %>
<%= f.text_field :pub_date, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :publisher %>
<%= f.text_field :publisher, class: 'form-control' %><br />
</div>
<div class="form-group">
<%= f.select(:author_id, @authors.collect {|a|
[ a.name, a.id ]}, {:include_blank => 'Please select an author'},
class: "form-control") %><br />
</div>
<%= f.submit 'Save Changes', class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
最后,我的 rake 路线:
Rake routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
author GET /author(.:format) authors#new
name GET /name(.:format) authors#show
book GET /book(.:format) books#new
show GET /show(.:format) books#show
edit GET /edit(.:format) books#edit
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
GET /books/:id(.:format) books#show
PATCH /books/:id(.:format) books#update
PUT /books/:id(.:format) books#update
DELETE /books/:id(.:format) books#destroy
authors GET /authors(.:format) authors#index
POST /authors(.:format) authors#create
new_author GET /authors/new(.:format) authors#new
edit_author GET /authors/:id/edit(.:format) authors#edit
GET /authors/:id(.:format) authors#show
PATCH /authors/:id(.:format) authors#update
PUT /authors/:id(.:format) authors#update
DELETE /authors/:id(.:format) authors#destroy
这是我的日志所说的:
在 2015-08-15 16:32:10 -0400 为 ::1 开始 PATCH "/book.18"
ActionController::RoutingError (没有路由匹配 [PATCH] "/book.18"):
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in call'
web-console (2.2.1) lib/web_console/middleware.rb:39:incall'
actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in call'
railties (4.2.1) lib/rails/rack/logger.rb:38:incall_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in block in call'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:inblock in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:intagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:incall'
机架 (1.6.4) lib/rack/methodoverride.rb:22:in call'
rack (1.6.4) lib/rack/runtime.rb:18:incall'
activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
【问题讨论】:
-
为什么你有所有这些额外的路线?您应该只使用
resources :authors和resources :books。 -
另外,当您发出导致该错误的请求时,您的 Web 控制台会说什么。看起来它正在保存,它应该重定向到
/books/17,但它正在获取/book.17。将控制台输出添加到问题中。 -
顺便说一句,你不需要
Book.find_by_id(params[:id]),你可以说Book.find(params[:id])。find本身暗示id。find_by_....的其他形式已弃用,取而代之的是find_by(column_name: 'something')。 -
我添加了我的日志并将 Book.find_by_id(params[:id]) 更改为 Book.find(params[:id])。我还注意到它在 /book.17 副 /book/17 之后。任何其他帮助将不胜感激。
标签: ruby ruby-on-rails-4 controller routes