0

link_to リンクを使用して登録コントローラーに投稿しようとしています。

私は持っている<%= link_to "Register for Period", registration_path(period_id: period.id), :method => :post %>

次のようなリンクが生成されますhttp://localhost:3000/registrations/6?period_id=25。6 は event_id です。period_id と user_id を登録データベースに保存する必要があります。

ブラウザに次のエラーが表示されます。No route matches [POST] "/registrations/6"

私は何を間違っていますか?

ルート:

Mavens::Application.routes.draw do

  devise_for :users

  resources :events
  resources :periods
  resources :products
  resources :cart_rows
  resources :product_requests
  resources :inqueries
  resources :registrations
   match '/profile',  to: 'static_pages#profile'

  root :to => 'static_pages#home'

  get "static_pages/home"
  get "static_pages/about"
end
4

2 に答える 2

1

あなたがあなたのを入れた場合routes.rb

resources :registrations do
  member do
    post :save_period
  end
end

そしてあなたのリンクで:

<%= link_to "Register for period", 
  save_period_registration_path(id: @registration.id, period_id: period.id), :method => :post %>

あなたはあなたの要求に一致するルートを持っているでしょう。にルール
しかない場合は、デフォルトのRESTfulルートのみが作成され、デフォルトで作成される単一のリソースはありません。 CSRFトークンについて何かを読む必要があると思います。なぜなら、にを持っている場合、おそらく単一のリンクからのこのリクエストは機能しないからです。resources :registrationsroutes.rbPOST
protect_from_forgeryapplication_controllerPOST

于 2012-08-17T12:34:54.910 に答える
0

ルート.rbに必要なパスがありません。これは標準のパスであるため、「resources:registrations」を追加すると機能します。

より複雑な場合は、routes.rbを投稿すると、何を追加するかがわかります。

于 2012-08-17T12:35:05.813 に答える