TheExit
TheExit

Reputation: 5320

Rails 3 - Helper, where are they supposed to be used?

I'm building a model, which whenever the model is run I want to inject into a news feed model. My idea was to create a after_save in the model and then use a helper to populate the feed details. But my model isn't finding the helper...

Now I'm wondering, what are helpers for, are helpers really just for Views or?

In my model I want to run stuff like: newsfeed.feeded_id = record.id newsfeed.feeded_type = record.class.name

Which is common to many models in my app. Where should that live, and how should that be accessed? Thanks

Upvotes: 1

Views: 525

Answers (2)

monocle
monocle

Reputation: 5896

How about using an observer?

Are you setting up your news via RSS/ATOM? Your builder view might be a place to select what items you want in your feed.

Upvotes: 0

Steve Ross
Steve Ross

Reputation: 4144

Helpers are primarily for views. They allow you to keep your code out of the views and make a testable chunk of code that can be used when rendering the view. It also allows you to reuse bits of code.

I don't think there are any rules of thumb, but if you use the exact same bit of view code more than once, consider abstracting it into a partial. If you have any complicated conditionals or calculations, consider placing it in a helper.

As an example of what's good to put in helpers, you need look no further than Rails itself. Perfect examples are link_to, form_for, etc. These take a bit of doing to construct but provide way more functionality than writing the HTML from scratch each time.

Upvotes: 1

Related Questions