Can we call a Controller's method from a view (as we call from helper ideally)?

Manish Shrivastava picture Manish Shrivastava · Jan 18, 2012 · Viewed 66.1k times · Source

In Rails MVC, can you call a controller's method from a view (as a method could be called call from a helper)? If yes, how?

Answer

sailor picture sailor · Jan 18, 2012

Here is the answer:

class MyController < ApplicationController
  def my_method
    # Lots of stuff
  end
  helper_method :my_method
end

Then, in your view, you can reference it in ERB exactly how you expect with <% or <%=:

<% my_method %>