1
2
)但不幸的是,他们没有帮助。
我有一个事件资源和一个命名空间event_参与者。路由、动作等看起来不错,但当我尝试单击指向自定义命名空间动作的链接时,路由映射到事件更新控制器。不知道出了什么事!
配置/路由.rb
Rails.application.routes.draw do
resources :events
namespace :events do
put "/:id/add_or_rm_role" => "participants#add_or_rm_role"
end
end
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!
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\u控制器。rb是自动生成的。
指向on events/show的链接是
<%= 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 %>
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"}
#......
知道这里有什么电线交叉吗?
以下答案建议使用嵌套路由而不是命名空间路由,但我希望继续尝试使用命名空间路由,以改进控制器中的组织/文件结构。切换到嵌套将是核选项,但我想看看这会把我带到哪里。利用当前反馈,我做了以下更改:
配置/路由.rb
#...
namespace :events do
put :add_or_rm_role, to: 'participants#add_or_rm_role'
end
耙路
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 %>
class Events::ParticipantsController < ApplicationController #Renamed
#...
def event_participants_params
params.permit(:role, :is_add, :event_id, :user_id)
end
end
尽管如此,控制器的#操作仍然是错误的(因此似乎正在添加
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\u或rm\u角色更高的优先级),则会匹配正确的控制器。这是因为我的事件ID是字符串吗?
namespace :events do
put :add_or_rm_role, to: 'participants#add_or_rm_role'
end
resources :events
NameError (uninitialized constant Participant):
更新
NameError(未初始化的常量参与者):
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]
作品