Rails : Access view/route helpers from anywhere (Model / Services)

Normally we always need to access route and view helper methods from anywhere in our code base.

View Helpers

In your model, you can do something like the following:

ApplicationController.helpers.your_helper_method

OR

YourController.helpers.your_helper_method

 

In console this will work in Rails ~= 2.3 and >= 3.1.0

app.post_path

# If namespacing is used in routes
app.spree.product_path(Spree::Product.first)
 

Path/Route helpers

class HelperWrapper
  include Rails.application.routes.url_helpers
end
  HelperWrapper.new.admin_path
Advertisements

Ruby On Rails : RuntimeError : Circular dependency detected

I faced this error and logged it so that any other who faces this error should not waste useful time on fixing this:

RuntimeError in Users::SessionsController#new

Circular dependency detected while autoloading constant SharedHelper

Extracted source (around line #1):

Ruby On Rails : Assets Precompile : How rails handles asset precompilation? How asset helpers find precompiled file names

If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array in config/initializers/assets.rb: : or simply inside application.rb

Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']