gpt4 book ai didi

ruby-on-rails - 命名空间资源 Controller 中的 POST 请求将执行索引操作而不是创建

转载 作者:行者123 更新时间:2023-12-04 16:59:03 24 4
gpt4 key购买 nike

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

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

路线.rb:

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

rake 路线:
             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 %>
...

应用程序/ Controller /管理员/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]

最佳答案

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

关于ruby-on-rails - 命名空间资源 Controller 中的 POST 请求将执行索引操作而不是创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/210513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com