How to redefine the route of the track in rails?

My route is defined as follows

match '/user/:id' => 'user#show', :as => :user 

If for some reason the identifier nil is passed, I want the route helper to return only '#', and if the ID is not nil, I want it to return a normal path, for example '/ user / 123'. or is there a better way to do this. this route helper has been used in many places in my code, so I don’t want to change it. Instead, I am looking for one place that will affect all instances of user_path.

thanks

+4
source share
2 answers
 module CustomUrlHelper def user_path(user, options = {}) if user.nil? "#" else super end end end # Works at Rails 4.2.6, for older versions try http://stackoverflow.com/a/31957323/474597 Rails.application.routes.named_routes.url_helpers_module.send(:include, CustomUrlHelper) 
+2
source

I did this in app/helpers/url_helper.rb

 module UrlHelper def resource_path(resource, parameters = {}) # my implementation "/#{resource.category}/#{resource.name}#{ "?#{parameters.to_query}" if parameters.present? }" end end 

I know that there are ways to define such a nested route, but I have a code base that uses this route in several parts, as the question says.

I tried using the old method, but was not recognized:

 alias_method :old_resource_path, :resource_path 
+1
source

All Articles