evans
evans

Reputation: 563

How to remove resource name from route helper?

I have a route:

resources :promo_pages, path: 'promo' do
    get :promo_rubizza, on: :collection, path: 'rubizza', as: :rubizza
end

This route is created - rubizza_promo_pages_path

But I'd like to have - rubizza_path.

How to implement it?
I wanted to implement this as resources :promo_pages, path: 'promo', as: '', but it created rubizza__index_path

UPD: output rails routes

rubizza_promo_pages GET    /promo/rubizza(.:format)       promo_pages#promo_rubizza

Upvotes: 1

Views: 333

Answers (3)

mmsilviu
mmsilviu

Reputation: 1451

Customising the routes names details are available here.

What about defining the path based on controller and action?

 get 'promo_pages/promo_rubizza', to: 'promo_pages#promo_rubizza', as: :rubizza

it will return:

 Prefix Verb URI Pattern                          Controller#Action
rubizza GET  /promo_pages/promo_rubizza(.:format) promo_pages#promo_rubizza

Upvotes: 0

MrShemek
MrShemek

Reputation: 2483

In routes, please define the following route (put it on the same level as resources, without nesting):

get 'promo/rubizza', to: 'promo_pages#promo_rubizza', as: 'rubizza'

Then, you should be able to use rubizza_path and rubizza_url.

Upvotes: 1

Saqib Shahzad
Saqib Shahzad

Reputation: 1002

what you can do is define this path in your application controller:

class ApplicationController < ActionController::Base
  ...
  def rubizza_path
    rubizza_promo_pages_path
  end

  helper_method :rubizza_path
end

in your routes.rb:

resources :promo_pages, path: 'promo' do
   get :promo_rubizza, on: :collection, path: 'rubizza', as: :rubizza
end

That way, you can still use rubizza_path in your views, helpers, controllers, etc. but it uses the full nested route instead.

Upvotes: 0

Related Questions