Breno
Breno

Reputation: 6282

Where should I create methods to use in my view?

Sorry for the newbie question, but I often wonder. Where should I create methods that I want to use in my View??

I'll illustrate. I want to call a method in my user/views/show.html.erb that will return a boolean and with that I can show a link on the show page, or not.

Well, I'll always thought that this method should be created at the User.rb, the model! But what I actually get is an undefined method error!

I would apreciate the help!

Thanks

UPDATE

It does work when I create the method in the User Helper.. But is that good programming? Is it the right way? Or is there a better way?

Upvotes: 0

Views: 115

Answers (3)

Breno
Breno

Reputation: 6282

Actually guys, it's pretty simple The newbie here didn't call its user method using the object: example: @user.my_metho I was just calling my_method

That was the problem.

But as Pedro mentioned the Draper gem really didn't do much for me... Couldn't even get it to work properly...

Upvotes: 0

Pedro Nascimento
Pedro Nascimento

Reputation: 13886

Nowadays I prefer to use a presenter, similar to what is shown in this Railscast. It keeps your code clean, reusable, and is more close to OO than Rails Helpers, which keeps you more sane. :)

Upvotes: 2

Carson Cole
Carson Cole

Reputation: 4451

I think that if its display-related code, it should probably go into the specific helper, or in 'application_helper' if you want it available in all views. Formatting and display logic (like what you have) should go into helpers.

If the method is going to make changes to your data, then it probably should go into a model. Not sure what you are getting an undefined method error, because it should work.

Upvotes: 0

Related Questions