【问题标题】:Rails 4 issue: No route matches [PATCH] "/book.17"Rails 4 问题:没有路线匹配 [PATCH] "/book.17"
【发布时间】: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 :authorsresources :books
  • 另外,当您发出导致该错误的请求时,您的 Web 控制台会说什么。看起来它正在保存,它应该重定向到/books/17,但它正在获取/book.17。将控制台输出添加到问题中。
  • 顺便说一句,你不需要Book.find_by_id(params[:id]),你可以说Book.find(params[:id])find 本身暗示 idfind_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


【解决方案1】:

一些复杂的解决方案……

线索是格式错误的 url “/book.17”,它应该是 /books/17。你把rails默认的url生成器和你的路由文件条目弄乱了

patch 'edit' => 'books#update'

因为

resources :books 

应该已经给你一个更新路线。但是,由于它是在下面(之后)定义的,您手动输入的补丁路线......这是获胜的路线,所以现在这条线在您的帮助表格中无法正常工作。更多信息请见this

form_for(@user)

另请注意,rails 中的“编辑”路线是呈现编辑视图的路线。它不是编辑表单提交的那个,那是更新操作。你有点尝试用

patch 'edit' => 'books#update'

您可能想要 book 路由,因为您要在 url 方案上进行强迫症,即它只有一本“书”,所以 url 不应该说“书”。别再这样想了。我的建议是,在你更好地掌握轨道之前,不要与惯例、复数路线等作斗争。最终这将是小菜一碟,但你还有很多事情要担心学习,所以不要浪费你的时间关于路由约定。只需喝一点kool-aid并学习框架。

如果你真的必须拥有它review this section of the rails routing guide

【讨论】:

    【解决方案2】:

    摆脱那些多余的路线,它们只会把你搞砸。 Rails 从上到下处理 routes.rb 文件,因此当您的路由基本上相互覆盖时,顺序可能会让您搞砸。只需使用:

    resources :books
    resources :authors
    

    这应该会给你一个rake:routes 输出,例如:

    books_path      GET /books(.:format)    books#index
                    POST    /books(.:format)    books#create
    new_book_path   GET /books/new(.:format)    books#new
    edit_book_path  GET /books/:id/edit(.:format)   books#edit
    book_path       GET /books/:id(.:format)    books#show
                    PATCH   /books/:id(.:format)    books#update
                    PUT /books/:id(.:format)    books#update
                    DELETE  /books/:id(.:format)    books#destroy
    

    控制器:

    class BooksController < ApplicationController
    before_action :set_book, only: [:show, :edit, :update, :destroy]
    # the above will DRY up your code with a callback
    
    def new
     #@book = Book.all
     @book = Book.new 
     @authors = Author.all
    end
    
    def edit 
      @authors = Author.all
    end 
    
    def update 
      if @book.update_attributes(book_params)
       flash[:success] = "Book Updated!"
       redirect_to @book
      else
      render 'edit'
      end
    
    private
    
      # Use callbacks to share common setup or constraints between actions.
      def set_book
        @book = Book.find(params[:id])
      end 
    end 
    

    【讨论】:

      【解决方案3】:

      改变路线

           patch 'edit' => 'books#update'
      

           patch '/book.:id/' => 'books#update' 
      

      解决问题

      【讨论】:

      • 完成但同样的错误“没有路由匹配 [PATCH]”/book.17”
      • 试着把这个放到 route.rb 文件 patch "/books/:id" => "books#update"
      • 将以下内容放入 route.rb: patch "/books/:id" => "books#update" 仍然得到错误信息: No route matches [PATCH] "/book.17"
      • 你能把代码上传到github让我看看吗?
      • 是的,它就在那里。我的 GitHub 名称是 ravenusmc,文件名为teacher_2
      猜你喜欢
      • 2021-02-27
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多