2016-09-01 34 views
0

この問題に関する多くの質問/回答を読んでいますが、私の修正が見つからないようです。ルーティングエラー - [POST]と一致するルートがありません

ここに問題があります。私は、注釈の単純な登録を作成するためのRailsの開始に従っています。フォームが機能します - 新しい&注釈を追加できます。

  • この:私は、インデックスへのリンクを追加する場合しかし、私は、ルーティングエラーが発生します<%= button_to "Details", annotation_path(annotation), :class => "btn btn-primary btn-xs"%>結果で:Noルートマッチ[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'はそれらのルートが' GET'の間に 'POST'リクエストをデフォルトで行います – kiddorails

+0

また、私はエラーログを慎重に調査することを強く勧めます。質問自体には、「POST」ルートが一致していないと表示されます。ルートが存在しないことを意味します。その理由を確認できます。 – kiddorails

+0

[ルーティングエラー - カスタムアクションでボタン\ _toを使用すると経路が一致しません]の重複可能性があります(http://stackoverflow.com/questions/12650213/routing-error-no-route-matches-when-using-button-to -with-custom-action) – kiddorails

答えて

0

を使用して変更:post GETでなければなりません。

私は(あなたがすでに必要なクラスを持っている)の代わりにCSSクラスとのlink_toを使用しますが、あなたは、あなたが望んでいた場合、それは以下の使用して行うことができますので、本当にGETリクエストのために使用すべきではありませんbutton_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

方法はデフォルトではbutton_toヘルパーは、実際のコード・レベルでフォームを生成し、したがって、フォーム、新しいリソースしかしながら経路を前記転記であるため、問題がある: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%> 
関連する問題