Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload Helper from Rails Console

I'm calling helper methods from the Rails console in Rails 3 like this:

>> helper.my_method(parameter)
>> #=> some result

However, if I change the helper method the changes are not reflected when I call the same method again. I have to exit and run rails console in order to see the changes to the helper method take effect.

like image 427
utiq Avatar asked Sep 29 '11 17:09

utiq


People also ask

What does reload do in Rails console?

Reload: This command will allow you to make changes to your code, and continue to use the same console session without having to restart. Simply type in the “reload!” command after making changes and the console will reload the session.

Can we use helper method in controller Rails?

In Rails 5, by using the new instance level helpers method in the controller, we can access helper methods in controllers.

How do you use the helper method in Ruby on Rails?

A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.

Why helper is used in Rails?

Basically helpers in Rails are used to extract complex logic out of the view so that you can organize your code better.


2 Answers

Actually, helper is an instantiated object that memoizes the ApplicationController helpers, which will not be reloaded when you call reload!, at least in Rails 4. You can work around this by calling ApplicationController.helpers.my_method(parameter) in the console. You will still need to use reload! when you edit the helper, but it will reload unlike helper.

like image 129
user3670743 Avatar answered Sep 30 '22 22:09

user3670743


You just need to run reload! and most classes will be reloaded, including your helpers.

like image 42
coreyward Avatar answered Sep 30 '22 21:09

coreyward