Jonathan Gill
2006-Jul-09 10:20 UTC
[Rails] 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 Gill -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 191 bytes Desc: This is a digitally signed message part Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060709/8c6606e8/attachment.bin
Hi Jonathan, Jonathan Gill 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
Jonathan Gill
2006-Jul-09 16:33 UTC
[Rails] using content from a model in application.rhtml
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 On Sun, 2006-07-09 at 09:48 -0500, Bill Walton wrote:> Hi Jonathan, > > Jonathan Gill 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Jonathan Gill +65 98551701 Chief Technology Officer +65 62436800 SecureCiRT Pte Ltd http://www.securecirt.com/ PGP : 315C 314D CD36 CBFF 728E F167 FCD8 15B7 0287 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 191 bytes Desc: This is a digitally signed message part Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060709/bc2ff527/attachment.bin