Ruby on rails 如何通过get方法强制传递所需的url路径
我有一条路线Ruby on rails 如何通过get方法强制传递所需的url路径,ruby-on-rails,Ruby On Rails,我有一条路线 collection do get :show_logs end 我希望用户应该请求显示日志/[:id] 禁止用户在没有所需id的情况下发送显示日志请求 有什么更好的方法得到它 更新 如果现在,我写下我的规则如下 并尝试在没有:id,http://localhost:3000/tool/mvaas/relay_queries/show_logs 我将在xxx中获得错误ActiveRecord::RecordNotFound 路线 您可以通过以下方式验证
collection do
get :show_logs
end
我希望用户应该请求显示日志/[:id]
禁止用户在没有所需id的情况下发送显示日志
请求
有什么更好的方法得到它
更新
如果现在,我写下我的规则如下
并尝试在没有:id
,http://localhost:3000/tool/mvaas/relay_queries/show_logs
我将在xxx中获得错误ActiveRecord::RecordNotFound
路线
您可以通过以下方式验证参数是否存在:
if(params.has_key?(:one))
如果存在-请求将完成。
如果缺席-重定向/渲染或显示通知。您可以尝试:
get "show_logs/:id" => "controller#action"
使用更新:只需编写以下内容:
namespace :tool do
namespace :mvaas do
resources :relay_queries do
collection do
get "show_logs/:id", action: "show_logs"
end
end
end
end
您应该将其放入
成员
而不是集合
resources :users do
member do
get :show_logs
end
end
可以通过url/users/:id/show_logs
如果您绝对希望url为/users/show_logs/:id
,那么您应该使用
get '/users/show_logs/:id', to: 'users#show_logs'
在您的
资源之前:用户执行
阻止您好,如果我遵循您的提示,我仍然会遇到一些问题。您的问题仍然存在吗?
get '/users/show_logs/:id', to: 'users#show_logs'