簡體   English   中英

Rails:路由錯誤:沒有路由與[PATCH]“ / things”匹配

[英]Rails: Routing Error: No route matches [PATCH] “/things”

我為模型“ Things”創建了一個編輯表單,但是在提交表單時,它返回錯誤“沒有路線與[PATCH]“ / things”匹配”。 奇怪的是,錯誤的URL是http://localhost:3000/things ,但是我希望它是http://localhost:3000/things/:id ,因為我在create下寫了redirect_to @thing功能。 我只是用資源:things定義了所有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

Things / 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>

耙路輸出

                 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

只需刪除:url => things_path並使用:

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

如果使用ressourcefull路由,則form_for幫助程序會自動生成正確的resfull端點。 取決於@thing被persisted? new_record? 端點將是:create或:update

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM