I'm working on ruby on rails and the following happens:
When rendering a page.html.slim, a method is launched from the controller thanks to the following command:
before_filter :metodo_que_hace_algo, only: [:pagina]
That command makes every time the page is rendered the method is executed.
Problem:
There are several partials (HTML snippets), what I want to know is how to do the same thing but when rendering a partial something like this:
before_filter :otro_metodo_que_hace_algo, only: [:_parcial]
I don't know how to achieve it and I'm new to ruby on rails... Please help
At the controller level, the existence of the partials that are going to be rendered at the view level is not known. What you could do is make those methods available to the view with
helper_method
, so in your controller you put:and then in the partial, you can call the method as you see fit:
If it's not what you expect to do, another way would be to handle the conditions to render one or the other partial at the controller level, make the method available as a helper, and use them both in your
before_filter
and in the view.