gespinha
gespinha

Reputation: 8477

How to get the route by helper name?

I have the following set of routes which point to the same view:

get 'mypath', to: 'home#mypath', as: 'mypath'
get 'mypath-v2', to: 'home#mypath', as: 'mypath_v2'
get 'mypath-v3', to: 'home#mypath', as: 'mypath_v3'

How can I check if I am using one route or the other inside the view?

For example if I want to get mypath-v2 or mypath_v2, how would I do it?

Upvotes: 2

Views: 40

Answers (2)

kimrgrey
kimrgrey

Reputation: 562

Well, as for me it is better to do such things using params. You can define your routes like this:

get "mypath/:version", :as => "mypath"

In this case you will be able to use params[:version] to clarify current path.

Upvotes: 1

blnc
blnc

Reputation: 4404

You would call it by appending _path or _url

For instance here is an example in a link:

<%= link_to 'Link to mypath-v2 page', mypath_v2_path %>

OR

<%= link_to 'Link to mypath-v2 page', mypath_v2_url %>

To compare them you can look at the request object. When you call request.path or request.fullpath it will bring in the actual address request path of the link.

So...

<%= if request.path.eql?('mypath-v2') ? "Used It" : "Other Route" %>

Upvotes: 0

Related Questions