【问题标题】:Rails: Routing Error: No route matches [PATCH] "/things"Rails:路由错误:没有路由匹配[PATCH]“/things”
【发布时间】:2014-10-08 20:40:58
【问题描述】:

我为我的模型“Things”创建了一个编辑表单,但是当我提交表单时,它返回错误“No route matches [PATCH] “/things””。奇怪的是,错误的 url 是http://localhost:3000/things,但我期待它是http://localhost:3000/things/:id,因为我在 create 函数下写了 redirect_to @thing。我刚刚用资源定义了我所有的事物路由:事物,所以它应该定义了更新路径。我做错了什么?

things_controller:

class ThingsController < ApplicationController
  def show
    @thing = Thing.find(params[:id])
    @category_things = CategoryThing.all
    @thing.categories.build
    @thing.things.build
    @related_things = RelatedThing.all
  end
  def index
  end
  def new
    @thing = Thing.new
    @things = Thing.all
  end
  def create
    @thing = Thing.new(thing_params)
    if @thing.save
      redirect_to @thing
    else
      render 'new'
    end
  end
  def edit
    @thing = Thing.find(params[:id])
  end
  def update
    @thing = Thing.find(params[:id])
    if @thing.update_attributes(thing_params)
      flash[:success] = "Thing updated"
      redirect_to @thing
    else
      render 'edit'
    end
  end

  private
    def thing_params
      params.require(:thing).permit(:name, :image_path, :avatar)
    end
end

东西/edit.html.erb:

<h1>Edit <%= @thing.name %></h1>
<p>
  <%= form_for @thing, :url => things_path, :html => { :multipart => true } do |f| %>

    <%= f.text_field :name, :placeholder => "Name of the thing" %>
    <br>
    <%= f.label :categories_the_thing_belongs_to %>
    <%= f.collection_select :categories, Category.all, :id, :name %>
    <br>
    <%= f.label :related_things %>
    <%= f.collection_select :related_things, Thing.all, :id, :name %>
    <br>
    <%= f.label :display_picture %>
    <%= f.file_field :avatar %>
    <br>
    <%= f.submit "Submit", class: "btn btn-primary" %>
  <% end %>
</p>

rake 路由的输出

                 Prefix Verb   URI Pattern                                        Controller#Action
            ratings_new GET    /ratings/new(.:format)                             ratings#new
         down_votes_new GET    /down_votes/new(.:format)                          down_votes#new
              thing_new GET    /thing/new(.:format)                               thing#new
      good_comments_new GET    /good_comments/new(.:format)                       good_comments#new
     good_comments_show GET    /good_comments/show(.:format)                      good_comments#show
       bad_comments_new GET    /bad_comments/new(.:format)                        bad_comments#new
     related_things_new GET    /related_things/new(.:format)                      related_things#new
             things_new GET    /things/new(.:format)                              things#new
    category_things_new GET    /category_things/new(.:format)                     category_things#new
      thing_ratings_new GET    /thing_ratings/new(.:format)                       thing_ratings#new
   category_ratings_new GET    /category_ratings/new(.:format)                    category_ratings#new
               subjects GET    /subjects(.:format)                                subjects#index
                        POST   /subjects(.:format)                                subjects#create
            new_subject GET    /subjects/new(.:format)                            subjects#new
           edit_subject GET    /subjects/:id/edit(.:format)                       subjects#edit
                subject GET    /subjects/:id(.:format)                            subjects#show
                        PATCH  /subjects/:id(.:format)                            subjects#update
                        PUT    /subjects/:id(.:format)                            subjects#update
                        DELETE /subjects/:id(.:format)                            subjects#destroy
          subjects_show GET    /subjects/show(.:format)                           subjects#show
     subject_things_new GET    /subject_things/new(.:format)                      subject_things#new
             categories GET    /categories(.:format)                              categories#index
                        POST   /categories(.:format)                              categories#create
           new_category GET    /categories/new(.:format)                          categories#new
          edit_category GET    /categories/:id/edit(.:format)                     categories#edit
               category GET    /categories/:id(.:format)                          categories#show
                        PATCH  /categories/:id(.:format)                          categories#update
                        PUT    /categories/:id(.:format)                          categories#update
                        DELETE /categories/:id(.:format)                          categories#destroy
     categories_results GET    /categories/results(.:format)                      categories#results
           subjects_new GET    /subjects/new(.:format)                            subjects#new
                   root GET    /                                                  home_page#home
         all_things_new GET    /all/things/new(.:format)                          things#new
          all_allthings GET    /all/allthings(.:format)                           all#allthings
    thing_good_comments GET    /things/:thing_id/good_comments(.:format)          good_comments#index
                        POST   /things/:thing_id/good_comments(.:format)          good_comments#create
 new_thing_good_comment GET    /things/:thing_id/good_comments/new(.:format)      good_comments#new
edit_thing_good_comment GET    /things/:thing_id/good_comments/:id/edit(.:format) good_comments#edit
     thing_good_comment GET    /things/:thing_id/good_comments/:id(.:format)      good_comments#show
                        PATCH  /things/:thing_id/good_comments/:id(.:format)      good_comments#update
                        PUT    /things/:thing_id/good_comments/:id(.:format)      good_comments#update
                        DELETE /things/:thing_id/good_comments/:id(.:format)      good_comments#destroy
     thing_bad_comments GET    /things/:thing_id/bad_comments(.:format)           bad_comments#index
                        POST   /things/:thing_id/bad_comments(.:format)           bad_comments#create
  new_thing_bad_comment GET    /things/:thing_id/bad_comments/new(.:format)       bad_comments#new
 edit_thing_bad_comment GET    /things/:thing_id/bad_comments/:id/edit(.:format)  bad_comments#edit
      thing_bad_comment GET    /things/:thing_id/bad_comments/:id(.:format)       bad_comments#show
                        PATCH  /things/:thing_id/bad_comments/:id(.:format)       bad_comments#update
                        PUT    /things/:thing_id/bad_comments/:id(.:format)       bad_comments#update
                        DELETE /things/:thing_id/bad_comments/:id(.:format)       bad_comments#destroy
          thing_ratings GET    /things/:thing_id/ratings(.:format)                ratings#index
                        POST   /things/:thing_id/ratings(.:format)                ratings#create
       new_thing_rating GET    /things/:thing_id/ratings/new(.:format)            ratings#new
      edit_thing_rating GET    /things/:thing_id/ratings/:id/edit(.:format)       ratings#edit
           thing_rating GET    /things/:thing_id/ratings/:id(.:format)            ratings#show
                        PATCH  /things/:thing_id/ratings/:id(.:format)            ratings#update
                        PUT    /things/:thing_id/ratings/:id(.:format)            ratings#update
                        DELETE /things/:thing_id/ratings/:id(.:format)            ratings#destroy
         thing_up_votes GET    /things/:thing_id/up_votes(.:format)               up_votes#index
                        POST   /things/:thing_id/up_votes(.:format)               up_votes#create
      new_thing_up_vote GET    /things/:thing_id/up_votes/new(.:format)           up_votes#new
     edit_thing_up_vote GET    /things/:thing_id/up_votes/:id/edit(.:format)      up_votes#edit
          thing_up_vote GET    /things/:thing_id/up_votes/:id(.:format)           up_votes#show
                        PATCH  /things/:thing_id/up_votes/:id(.:format)           up_votes#update
                        PUT    /things/:thing_id/up_votes/:id(.:format)           up_votes#update
                        DELETE /things/:thing_id/up_votes/:id(.:format)           up_votes#destroy
                 things GET    /things(.:format)                                  things#index
                        POST   /things(.:format)                                  things#create
              new_thing GET    /things/new(.:format)                              things#new
             edit_thing GET    /things/:id/edit(.:format)                         things#edit
                  thing GET    /things/:id(.:format)                              things#show
                        PATCH  /things/:id(.:format)                              things#update
                        PUT    /things/:id(.:format)                              things#update
                        DELETE /things/:id(.:format)                              things#destroy
            things_show GET    /things/show(.:format)                             things#show
         things_results GET    /things/results(.:format)                          things#results
          things_random GET    /things/random(.:format)                           things#random
            web_console        /console                                           WebConsole::Engine

Routes for WebConsole::Engine:
                          root GET    /                                              web_console/console_sessions#index
         input_console_session PUT    /console_sessions/:id/input(.:format)          web_console/console_sessions#input
pending_output_console_session GET    /console_sessions/:id/pending_output(.:format) web_console/console_sessions#pending_output
 configuration_console_session PUT    /console_sessions/:id/configuration(.:format)  web_console/console_sessions#configuration
              console_sessions GET    /console_sessions(.:format)                    web_console/console_sessions#index
                               POST   /console_sessions(.:format)                    web_console/console_sessions#create
           new_console_session GET    /console_sessions/new(.:format)                web_console/console_sessions#new
          edit_console_session GET    /console_sessions/:id/edit(.:format)           web_console/console_sessions#edit
               console_session GET    /console_sessions/:id(.:format)                web_console/console_sessions#show
                               PATCH  /console_sessions/:id(.:format)                web_console/console_sessions#update
                               PUT    /console_sessions/:id(.:format)                web_console/console_sessions#update
                               DELETE /console_sessions/:id(.:format)                web_console/console_sessions#destroy

【问题讨论】:

    标签: ruby-on-rails ruby post ruby-on-rails-4 routes


    【解决方案1】:

    只需删除 :url =&gt; things_path 并使用:

    <%= form_for @thing, :html => { :multipart => true } do |f| %>
    

    如果使用 ressourcefull 路由,form_for 助手会自动生成正确的 resfull 端点。 根据@thing 是persisted?new_record?,端点将是 :create 或 :update

    【讨论】:

    • 早该知道就这么简单。谢谢!
    • 思想只有点赞才需要信用。接受自己问题的答案应该是可能的
    • @JoeMorano 请接受这个答案,它也帮助了我 FWIW
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多