2016-09-01 138 views
0

已閱讀關於此問題的許多問題/答案,但似乎找不到我的修復程序。路由錯誤 - 沒有路由匹配[POST]

這是問題:我正在跟蹤Rails的入門以創建一個簡單的註釋註冊表。我的表單工作 - 可以添加新的&更新註釋。然而,當我將鏈接添加到索引中,我得到一個路由錯誤:

  • 此:<%= button_to "Details", annotation_path(annotation), :class => "btn btn-primary btn-xs"%>結果:沒有路由匹配[POST]「/註釋/ 5」
  • 此:<%= button_to "Add Annotation", new_annotation_path, :class => "btn btn-primary btn-xs"%>沒有路由匹配[POST] 「/註釋/新」

感謝您的幫助

Routes.db:

Rails.application.routes.draw do 
    root 'dashboard#index' 
    devise_for :users 
    resources :users, :annotations 

控制器:

class AnnotationsController < ApplicationController 

    def index 
     @annotations = Annotation.all 
    end 

    def show 
     @annotation = Annotation.find(params[:id]) 
    end 

    def new 
     @annotation = Annotation.new 
    end 

    def edit 
     @annotation = Annotation.find(params[:id]) 
    end 

    def create 
     @annotation = Annotation.new(annotation_params) 

     @annotation.save 
     redirect_to @annotation 
    end 

    def update 
     @annotation = Annotation.find(params[:id]) 

     if @annotation.update(annotation_params) 
      redirect_to @annotation 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @annotation = Annotation.find(params[:id]) 
     @annotation.destroy 

     redirect_to annotations_path 
    end 

    private 
     def annotation_params 
      params.require(:annotation).permit(:name, :description) 
     end 
end 

形式(=部分)

<%= simple_form_for @annotation, url: annotations_path, html: { class: 'form-horizontal' }, 
    wrapper: :horizontal_form, 
    wrapper_mappings: { 
     check_boxes: :horizontal_radio_and_checkboxes, 
     radio_buttons: :horizontal_radio_and_checkboxes, 
     file: :horizontal_file_input, 
     boolean: :horizontal_boolean 
    } do |f| %> 

    <%= f.error_notification %> 

    <%= f.input :name, placeholder: 'Enter name' %> 

    <%= f.input :description, placeholder: 'Description' %> 

    <%= f.input :file, as: :file %> 

    <%= f.input :active, as: :boolean %> 

    <%= f.input :choice, as: :check_boxes, 
    collection: [ 
     'Option one ...', 
     'Option two ...'] %> 

    <%= f.input :documenttype, as: :radio_buttons, 
    collection: ['Type1', 'Type2'] %> 

    <%= f.button :submit %> 

<% end %> 

注形式:無濟於事,我試圖使用<%= simple_form_for :annotation, url: annotations_path,

+0

加上'方法:GET'。 'button_to'在默認情況下執行'POST'請求​​,而你的路由是'GET' – kiddorails

+0

另外,我堅持仔細研究錯誤日誌。這個問題本身說'POST'路線不匹配,這意味着路線不存在,你可以檢查爲什麼是這種情況。 – kiddorails

+0

[Routing Error - 路由匹配時使用按鈕\ _to與自定義操作]的可能重複(http://stackoverflow.com/questions/12650213/routing-error-no-route-matches-when-using-button-to -with-custom-action) – kiddorails

回答

0

問題是因爲button_to助手實際上產生在代碼級別形式,因此是發帖稱的形式,但是對於新資源路線必須是GET。

的button_to標籤真的不應該使用GET請求,所以我會使用帶有CSS班的link_to代替(你已經有了必要的類),但您可以在使用下面的,如果你想要做的事:

<%= button_to "Details", annotation_path(annotation), class: "btn btn-primary btn-xs", method: :get%> 
然而

更好的方法是:

<%= link_to "Details", annotation_path(annotation), class: "btn btn-primary btn-xs" %> 
+0

更好的方法的好建議。那麼我應該在什麼情況下使用button_to?僅用於發佈? –

+0

我會說你也應該使用它來進行PUT \ PATCH(更新)動作,但是這需要在輔助程序上指定方法,如上面的i/e:patch或:put。 – RichardAE

0

在此處閱讀文檔button_to

的方法是通過默認:post使用:method PARAM改變它

<%= button_to "Details", annotation_path(annotation), :class => "btn btn-primary btn-xs", method: :get%> 

<%= button_to "Add Annotation", new_annotation_path, :class => "btn btn-primary btn-xs", method: :get%> 
相關問題