我有两条路线要指向同一控制器动作。
get '/events/:url_token' , to: 'events#show'
get '/tv/events/:url_token', to: 'events#show'
,并在我的EventsContoller中执行show动作,我将事件设置为
@event = Event.find_by(url_token: params['id'])
当我导航到/ events / asdf时,这很好用,因为参数看起来像
{“ id” =>“ asdf”}
但是当我导航到/ tv / events / asdf时,我会得到
{“ url_token” =>“ hbbnHw”}
我不明白为什么这两个路由的参数名称不同或我应该如何解决。
编辑:我在与事件相关的路线中也有此提示
resources :events do
member do
post :addeventgroup
patch :updateeventtype
patch :updateeventgroup
patch :start_event
end
答案 0 :(得分:7)
resources :events
将多个路由添加到您的应用程序-其中之一类似于以下模式:
get '/events/:id', to: 'events#show'
当您不希望使用该路由时,而是想使用通过
定义的路由get '/events/:url_token' , to: 'events#show'
然后您可以像这样禁用resources :events
生成的路由:
resources(:events, except: :show) do
member do
# ...
end
end
我建议阅读Rails Guides about Routing。
或者,您可以仅删除此get '/events/:url_token', to: 'events#show'
定义,然后在另一个参数中重命名参数,如下所示:
get '/tv/events/:id', to: 'events#show'