gpt4 book ai didi

ruby-on-rails - 命名空间路由到错误的(父) Controller 操作

转载 作者:行者123 更新时间:2023-12-04 17:02:14 24 4
gpt4 key购买 nike

还有一些其他问题( 1 2 )但不幸的是它们没有帮助。

我有一个事件资源和一个命名空间的 event_participant。路由、操作等看起来不错,但是当我尝试单击 link_to 到我的自定义命名空间操作时,路由被映射到 events#update Controller 。不知道出了什么问题!

配置/路由.rb

Rails.application.routes.draw do
resources :events
namespace :events do
put "/:id/add_or_rm_role" => "participants#add_or_rm_role"
end
end

rake 路线
          Prefix Verb   URI Pattern                          Controller#Action
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PATCH /events/:id(.:format) events#update
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
PUT /events/:id/add_or_rm_role(.:format) events/participants#add_or_rm_role #This one looks good!

参与者_ Controller .rb
class ParticipantsController::EventsController < ApplicationController
load_and_authorize_resource
def add_or_rm_role
event = Event.find(event_participants_params.event_id)
#add user role to event...
redirect_to event, notice: 'Changes have been saved!'
end

private
def event_participants_params
params.permit(:role, :is_add, :event_id, :user_id)
end
end

events_controller.rb 是自动生成的。

事件/节目上的 link_to 是
    <%= link_to "Sign Up For Event", 
event_path(role: Event::Participant.event_role_types[:participant], is_add: false, id: @event.id),
action: :add_or_rm_role,
method: :put, remote: true %>

当我单击该链接时,会发送以下 PUT 请求。您可以看到它正在将错误的链接处理为错误的格式。 link_to 是一个小按钮,用于将 current_user 作为参与者添加到给定事件。
Started PUT "/events/_ceEY?is_add=false&role=3" for 127.0.0.1 at 2017-07-31 20:05:10 -0400
Processing by EventsController#update as JS
Parameters: {"is_add"=>"false", "role"=>"3", "id"=>"_ceEY"}
#......

知道什么电线在这里交叉吗?

更新

以下答案建议使用嵌套路由而不是命名空间路由,但我想继续尝试使用命名空间路由来改进我的 Controller 中的组织/文件结构。切换到嵌套将是核心选项,但我想先看看这会把我带到哪里。利用当前的反馈,我进行了以下更改:

配置/路由.rb
#...
namespace :events do
put :add_or_rm_role, to: 'participants#add_or_rm_role'
end

rake 路线
              Prefix Verb   URI Pattern                      Controller#Action
#...
events_add_or_rm_role PUT /events/add_or_rm_role(.:format) events/participants#add_or_rm_role

链接到
    <%= link_to "Sign Up For Event", 
events_add_or_rm_role_path(role: Event::Participant.event_role_types[:participant], is_add: false, event_id: @event.id),
method: :put, remote: true %>

事件/参与者_controller.rb
class Events::ParticipantsController < ApplicationController #Renamed
#...
def event_participants_params
params.permit(:role, :is_add, :event_id, :user_id)
end
end

尽管如此,controller#action 仍然是错误的(因此似乎添加了 id 参数)
Started PUT "/events/add_or_rm_role?event_id=_ceEY&is_add=false&role=3" for 127.0.0.1 at 2017-07-31 21:37:45 -0400
Processing by EventsController#update as JS
Parameters: {"event_id"=>"_ceEY", "is_add"=>"false", "role"=>"3", "id"=>"add_or_rm_role"}

我哪里出错了(服务器重新启动)?

更新

如果我更改 Rails 路由的顺序(赋予 add_or_rm_role 更高的优先级),则会匹配正确的 Controller 。这是因为我的事件 ID 是字符串吗?
  namespace :events do
put :add_or_rm_role, to: 'participants#add_or_rm_role'
end
resources :events

现在调用了正确的 Action ,但在它完成之前抛出了一个错误: NameError (uninitialized constant Participant):
更新
NameError (uninitialized constant Participant):被抛出是因为我试图在 Events::ParticipantsController 顶部授权(通过 CanCanCan)一个非资源 load_and_authorize_resource .由于模型是命名空间的,我需要手动指定资源模型: load_and_authorize_resource class: "Event::Participant"
更新

修复上述所有问题后,我收到错误 NoMethodError (undefined method 'event_id' for #<ActionController::Parameters:0x007f791dcbe240>): ,我认为这与强参数有关,但最终是由使用点表示法引起的: event_participants_params.event_id坏了但是 event_participants_params[:event_id]作品。

最佳答案

看看你的路线

PUT    /events/:id/add_or_rm_role(.:format) events/participants#add_or_rm_role #This one looks good!
events/participants#add_or_rm_role转换为名为 add_or_rm_role 的操作在名为 Events::ParticipantsController 的命名空间 Controller 上,但是这个 Controller 不存在。

你确实有一个 ParticipantsController::EventsController ,但我认为这也不是您的意图。这转化为 EventsController嵌套在 ParticipantsController 内的类模块。我会简单地将其重命名为 ParticipantsController .

您似乎还想发送 id: @event.id到 Controller ,但 Controller 需要一个名为 event_id 的参数.

我认为您正在寻找 nest这条路径,不是 namespace它。我认为对您的路线的这种更改将使您走上正确的 rails
Rails.application.routes.draw do
resources :events do
put '/add_or_rm_role', as: :add_or_rm_role, to: 'participants#add_or_rm_role'
end
end

它将生成这条路线
event_add_or_rm_role PUT    /events/:event_id/add_or_rm_role(.:format) participants#add_or_rm_role

您现在将拥有一个路由辅助方法 event_add_or_rm_roleevent_id URL 中的参数,将放置请求发送到 ParticipantsController#add_or_rm_role .希望这可以帮助

关于ruby-on-rails - 命名空间路由到错误的(父) Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45427145/

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