peeyush singla peeyush singla - 1 year ago 163
Ruby Question

How to get the current route in rails

I am building some rails sample application in which I am having two models User and Projects. Association between both is user has_many projects. Now the question is I am willing to provide some drop down for user's projects on top and if some one click on that one it will take that to project show page. But if user is at edit page of one project and select other project from dropdown I want him to reach edit page of new selected project same case for show page any idea. The solution I am looking for is e.g:- we can find current controller using params[:controller], find current action using params[:action] how can I find current route.
Thanx in advance

If someone want to see what is my code:- here is the link for github:-
' Here I am using simple link but actually I will provide some single drop down that will be displayed that will work for all the edit, show, new every action

Answer Source

To retrieve the URLs:

# Current URL:

# Current relative URL:

Additionally, you can check with the current_page method whether you are in a certain route.

# => true / false

For Rails 4 use:

# Current URL:

# Current relative URL:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download