mattC
mattC

Reputation: 373

Rails - Multiple routes for the same model

I have a Post model which belongs to a Coin model. 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?

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

Upvotes: 1

Views: 125

Answers (1)

mrzasa
mrzasa

Reputation: 23347

You can do that by checking presence of coin_id in the params:

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

Upvotes: 1

Related Questions