How to define your own routing helpers on rails 3?

I am using polimorphic_path and this is some error. This method requires some auxiliary route that is not defined. How can I define (for example, a regular method) my own route helper that will be used as "model_name_path, model_name_url, etc."?

+5
source share
2 answers

This solution worked for me.

Add this code to the end of the file config/routes.rb. Be sure to replace MyAppwith your application name.

MyApp::Application.routes.named_routes.module.module_eval do
  def model_name_path(*args)
    # Your code here
  end

  def model_name_url(*args)
    # Your code here
  end
end

MyApp::Application.routes.named_routes.instance_eval do
  @helpers += [:model_name_path, :model_name_url]
end

These custom methods will be available in controllers, views, and tests.

+4
source

_path, _url. - , ?

# at the bottom of config/routes.rb
module ActionView::Helpers::UrlHelper
    def model_name_path model, args={}
        # your implementation
    end
end
+2

All Articles