createではなくindexアクションを実行する名前空間リソースコントローラーのPOST要求

StackOverflow https://stackoverflow.com/questions/210513

  •  03-07-2019
  •  | 
  •  

質問

一部の管理機能用の名前空間コントローラーがあります。私の作成フォームは機能しません。作成アクションではなく、インデックスアクションにリクエストをルーティングすることになります。

POSTが作成アクションにルーティングされるのはなぜですか(RESTfulである)?

routes.rb:

  map.namespace :admin do |admin|
    admin.resources :events
  end

レーキルート:

             admin_events GET    /admin/events                   {:action=>"index", :controller=>"admin/events"}
    formatted_admin_events GET    /admin/events.:format           {:action=>"index", :controller=>"admin/events"}
                               POST   /admin/events                   {:action=>"create", :controller=>"admin/events"}
                               POST   /admin/events.:format           {:action=>"create", :controller=>"admin/events"}
           new_admin_event GET    /admin/events/new               {:action=>"new", :controller=>"admin/events"}
 formatted_new_admin_event GET    /admin/events/new.:format       {:action=>"new", :controller=>"admin/events"}
          edit_admin_event GET    /admin/events/:id/edit          {:action=>"edit", :controller=>"admin/events"}
formatted_edit_admin_event GET    /admin/events/:id/edit.:format  {:action=>"edit", :controller=>"admin/events"}
               admin_event GET    /admin/events/:id               {:action=>"show", :controller=>"admin/events"}
     formatted_admin_event GET    /admin/events/:id.:format       {:action=>"show", :controller=>"admin/events"}
                               PUT    /admin/events/:id               {:action=>"update", :controller=>"admin/events"}
                               PUT    /admin/events/:id.:format       {:action=>"update", :controller=>"admin/events"}
                               DELETE /admin/events/:id               {:action=>"destroy", :controller=>"admin/events"}
                               DELETE /admin/events/:id.:format       {:action=>"destroy", :controller=>"admin/events"}

app / views / admin / events / new.html.erb:

<h1>New event</h1>

<% form_for([:admin, @event]) do |f| %>
  <%= f.error_messages %>
  ...

app / controllers / admin / event_controller.rb:

class Admin::EventsController < ApplicationController
  def index
    @events = Event.find(:all)
    ...
  end

  def create
    @event = Event.new(params[:event])
    ...
  end

  ...
end

そして最後に、それを見ることができるちょっとしたログファイルが実際にPOSTしています:

Processing Admin::EventsController#index (for 127.0.0.1 at 2008-10-16 18:12:47) [POST]
  Session ID: ...
  Parameters: {"commit"=>"Create", "authenticity_token"=>"...", "action"=>"index", "controller"=>"admin/events", "event"=>{"location"=>""}}
  Event Load (0.000273)   SELECT * FROM `events` 
Rendering template within layouts/application
Rendering admin/events/index
Completed in 0.00757 (132 reqs/sec) | Rendering: 0.00118 (15%) | DB: 0.00027 (3%) | 200 OK [http://localhost/admin/events]
役に立ちましたか?

解決

ルートの順序が問題でした。正確な理由はわかりませんが、ルートルート( map.connect '' )の下に移動すると問題が解決し、Railsはそれに応じてリクエストをルーティングします。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top