Customizing resource routes for every action

50 Views Asked by At

I have a resources :posts. How can I customize it's paths to the following and also ability to call it with the normal resource path names:

URL          Controller action      Helper function
'q'          'posts#index'          posts_path
'q/(:id)'    'posts#show'           post_path(:id)
'ask'        'posts#new'            new_post_path
'q'          'posts#create'         posts_path

Here is what I've tried and doesn't work as the expected result above...

get 'q' => 'posts#index', as: :posts
get 'q/(:id)' => "posts#show", as: :post
get 'ask' => 'posts#new'
2

There are 2 best solutions below

0
On

You're presumably getting an error because you're trying to assign a route name that is already in use.

The resources posts call results in a definition for the posts and post routes. If you change your as: .. clause to reference different (unused) names, you will no longer get that error.

0
On

try resources :posts path => 'q'