Using content from a model in application.rhtml

Hi all,

Not sure if Im doing this the right way (most likely not as I cant make
it work!)

I have an application that needs to put some content from two different
tables(db) on every page within the application.

What I thought I could do is write a small application helper that talks
to the model and asks for the data. But I cant for the life of me work
out how to actually talk to a model from an application helper.

So first, is this the right way to do it? And if so, how would I
interact with the model from the application helper?

Thanks

Jonathan G.

Hi Jonathan,

Jonathan G. wrote:

I have an application that needs to put some content
from two different tables(db) on every page within the
application.

What I thought I could do is write a small application
helper that talks to the model and asks for the data.
But I cant for the life of me work out how to actually
talk to a model from an application helper.

So first, is this the right way to do it? And if so, how
would I interact with the model from the application
helper?

Helpers are probably part of your solution, but helpers assume that a
controller is ‘passing them’ the data they act on. So the short answer
to
your last question is “no, helpers don’t interact directly with the
model.”
If the controller code that feeds the views is the same in each
controller,
you can put that code in application.rb to keep your controllers DRY.
Then
you can use a helper to keep your views DRY too.

hth,
Bill

Hi Bill

Thanks for the mail. Ive tried to expand on what you have said, but I
still cant quite grasp it.

I think I am just not seeing how to do this sort of thing. Could you
point me to some docs somewhere that explain how to work with the
application_controller? Ive been through the AWDWR2 book and its pushed
me closer, but still no luck.

This is what I have so far.

In application.rhtml it sets the layout for the entire site, I have a
portion in there that I want to pull from a database. So I thought I
should just write a helper to do this. But you mention that helpers dont
have access to models so, I guess I need to drop some code into the
application.rb file that talks to the method I have in the model that
gets me the data. Now I have tried this, but I cant see how to do it!

So Im stumped, and really need pointing in the right direction.

Many thanks

Jonathan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs