Requête POST dans le contrôleur de ressources namespaced allant à l'action d'index au lieu de créer

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

  •  03-07-2019
  •  | 
  •  

Question

J'ai un contrôleur namespaced pour certaines fonctionnalités de l'administrateur. Mon formulaire de création ne fonctionne pas - il finit par acheminer la demande vers l'action d'indexation au lieu de l'action de création.

Pourquoi le POST n'est-il pas acheminé vers l'action de création comme il se doit (RESTful)?

routes.rb:

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

rake routes:

             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

Et enfin, un petit fichier journal dans lequel vous pouvez voir qu’il s’agit bien d’un 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]
Était-ce utile?

La solution

L’ordre des itinéraires était le problème. Je ne sais pas exactement pourquoi, mais le déplacer sous mes routes racine ( map.connect '' ) résout le problème et Rails achemine les demandes en conséquence.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top