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?
You generally don't call controller-methods from helpers. That is: if you mean a method that collects data and then renders a view (any other method that needs to be called should probably not be in a controller). It is definitely bad practice and breaks MVC.
Yes, you can call a method of another controller. The controller is also a simple class.
As the two answers said, you should not be calling controller methods from your models. It is not recommended.
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With