0

我有一个用于某些管理功能的命名空间控制器。我的创建表单不起作用——它最终将请求路由到索引操作而不是创建操作。

为什么 POST 没有按应有的方式路由到创建操作(RESTful)?

路线.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"}

应用程序/views/admin/events/new.html.erb:

<h1>New event</h1>

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

应用程序/控制器/管理员/event_controller.rb:

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

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

  ...
end

最后,您可以看到它确实正在发布的一些日志文件:

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]
4

1 回答 1

4

路线的顺序是问题。我不确定为什么,但是将它移到我的根路由 ( map.connect '') 下可以解决问题,并且 Rails 会相应地路由请求。

于 2008-10-16T22:40:45.097 回答