Wednesday, December 29, 2010

Thursday, December 9, 2010

Monday, December 6, 2010

Accessing Helper modules in rails

by sandipransing 0 comments

Methods defined in Helper modules can directly accessed in rails views because this is what they are pretended for but we often come across with situations where we wanted to use some helper methods in controllers, views, models and mailers and obvious we don't want to repeat same lines of code everywhere which also rail does not permit. forgotten DRY? Oh then how to do achieve same without violating rails principle.

Certainly there are ways to do this ..


1. Helper methods all the time for views
class ApplicationController < ActionController::Base
helper :all# include all helpers, all the time for views
end

2. Controller methods in views
class ApplicationController < ActionController::Base
helper_method :current_store
#now controller_method can be accessed in views
end

2. Helper methods in controller
class ApplicationController < ActionController::Base
include ActionView::Helpers::ApplicationHelper
end

3. Helper methods in model
class Student < ActiveRecord::Base
include ActionView::Helpers::ApplicationHelper
end

4. Helper methods in mailer
class Notifier < ActionMailer::Base
add_template_helper(ApplicationHelper)
#...
end
Read More…

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter