简体   繁体   English

Rails - 同一模型的多条路线

[英]Rails - Multiple routes for the same model

I have a Post model which belongs to a Coin model.我有一个属于 Coin 模型的 Post 模型。 Is it possible to also use the Post model on its own and have a conditional in the controller that performs different actions based on if the route is /coins/posts or just /posts?是否也可以单独使用 Post 模型,并在控制器中有一个条件,根据路由是 /coins/posts 还是只是 /posts 执行不同的操作?

My routes are set up as我的路线设置为

resources :posts
resources :coins do
  resources :posts 
end

Is it possible to some sort of check like是否有可能进行某种检查

if (the current route is coin_posts_path)
  do x
elsif (the current route is posts_path)
  do y
end

You can do that by checking presence of coin_id in the params:您可以通过检查参数中coin_id存在来做到这一点:

if params[:coin_id]
  # nested action
else
  # standalone action
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM