simo
simo

Reputation: 24550

rails 3.1, how to use before_filter to call method at a helper?

I need to call a method defined in a helper which uses content_for, I need to render some buttons& links at lots of views.

The question is, how can I benefit from the before_filter ? I know its not designed for helpers, so, what's the proper way to do so ?

Currently, I am calling the helper method at most of views, which seems not practical.

Upvotes: 1

Views: 1512

Answers (2)

nathanvda
nathanvda

Reputation: 50057

A before_filter will not be of any help here. A before_filter is used inside the controller, to execute method before starting an action. Generally, what one does:

  • verify authentications/access/authorisations
  • retrieve data
  • it can also be used to refactor code that is shared between controller methods

In your case, what I would consider are:

  • using a partial, to share the common view-part
  • use different layout, instead of application.html.erb to group the shared views together
  • push the shared view-part to application.html.erb

Hope this helps.

Upvotes: 3

Alex Peattie
Alex Peattie

Reputation: 27637

If you're calling a method in a bunch of views, you probably want to put it in the application layout (by default app/views/layouts/application.html.erb)

Upvotes: 1

Related Questions