Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias route's name

I need to have one path accessible through multiple names. In my routes.rb I did

get '/route' => 'controller#edit', :as => 'name_a'
get '/route' => 'controller#edit', :as => 'name_b'

This works nicely but loads the routes table for nothing. From my understanding of the documentation, :as defines a helper method when called.

So I went to my ApplicationController and added

alias_method :name_b, :name_a

and I removed the second line from routes.rb

but that fails with Uncaught exception: undefined method name_a for class ApplicationController

is there any proper way of having two names for a single path?

=================EDIT==================== Elaboration:

I use Devise gem to manage session, registration, locking, etc. of 2 kinds of users, let's call them Admin and Guest. The gem is very well put but it asks for definitive route names to behave properly.

In my case, as far as devise is concerned, only the registration process is different so I'm trying to build a structure which looks as follow:

app
  controllers
    users
        admin
            registration_controller.rb
        guest
            registration_controller.rb
        session_controller.rb
        password_controller.rb
        registration_controller.rb

the Admin and Guest controllers inherit from the above registration_controller which inherit's from Devise.

Now, to work properly, Devise needs for instance the names guest_user_password and admin_user_password to create or delete password retrievals. In my case, both are under the same path so I want both names to redirect to the same 'users/password' controller.

More important, and that's why I really wanted the alaising. Is that my views should not care whether it is dealing with Admin and Guest routes when redirecting to password retrieval controller. Both are users so I want to use user_password for both.

Hence my question. :)

Also note that as I wrote it, things works. I'm just trying to get the 'most elegant way' of writing it.

like image 299
muichkine Avatar asked Apr 04 '13 20:04

muichkine


2 Answers

How about putting the alias in your ApplicationController?

class ApplicationController < ActionController::Base

alias_method :route_new, :route_old
helper_method :route_new

Remember that it's new name first, then old name.

The helper_method call is in order to use these in your views and not just controllers.

If you like, you can then place this in an included module called something like RouteAliases

like image 145
Houen Avatar answered Nov 12 '22 04:11

Houen


You can add something like this to your routes.rb:

Rails.application.routes.draw do

  ...

  Rails.application.routes.named_routes.tap do |named_routes|
    named_routes['new_name'] = named_routes['real_name']
  end
end

This will create new_name_path and new_name_url helpers. I have tested this with Rails 5.0.6.

like image 26
Julia Path Avatar answered Nov 12 '22 05:11

Julia Path