Hi All How do I access a model from within the application.rb? What im tying to do is pull some data from a model (news in this case) that must be displayed on each page within the entire application. I know I could drop this into each controller, have each view pull it from the controller, but thats repeating myself... So whats the best way to do this (as I dont think models are visible from within application.rb)? Many thanks Jonathan -------------- 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/20060710/6678f1e0/attachment.bin
On Mon, Jul 10, 2006, Jonathan Gill wrote:> Hi All > > How do I access a model from within the application.rb? > > What im tying to do is pull some data from a model (news in this case) > that must be displayed on each page within the entire application. I > know I could drop this into each controller, have each view pull it from > the controller, but thats repeating myself... > > So whats the best way to do this (as I dont think models are visible > from within application.rb)?Sure they are. Consider application.rb to be just another controller that happens to get included in all of your other controllers. Just access it like normal and you should be fine. Ben -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 155 bytes Desc: Digital signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060710/1c60612c/attachment.bin
On 7/10/06, Jonathan Gill <jonathan.gill@securecirt.com> wrote:> > Hi All > > How do I access a model from within the application.rb? > > What im tying to do is pull some data from a model (news in this case) > that must be displayed on each page within the entire application. I > know I could drop this into each controller, have each view pull it from > the controller, but thats repeating myself... > > So whats the best way to do this (as I dont think models are visible > from within application.rb)?application.rb is a controller that all other controllers inherit from. Models are just as visible in the application controller as any other. There are a couple of ways to do this. Helper methods, before_filters and a couple of more obscure ones. Probably using both would be benificial. You can define the method in the controller and then tell it to act as a helper method as well. Then you get it in the controller and the views. First, use a before_filter in the application controller class ApplicationController < ActionController::Base before_filter :set_some_variable helper_methods :some_variable def some_variable @variable end protected def set_some_variable @variable = MyModel.find(:first) end end This will provide a method some_variable to both views and controllers. The @variable will be set every time the controller is fired up. Hope this helps Cheers Many thanks> > Jonathan > > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2.2 (GNU/Linux) > > iD8DBQBEseUL/Nj+exW3AocRAm6dAKDGSK5Yv+s7Ih1aHoBsVo20wM5NMACfQVT8 > JRWQzUXPexrtujy1/NcdeN8> =6K+t > -----END PGP SIGNATURE----- > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060710/341ee9bb/attachment-0001.html
Hi All, Just to report back, I worked out the problem I had. Just needed to read more and get pushed in the right direction. I ended up doing it with a partial that pulls uses an object pulled from the model. Thanks to all for the push in the right direction Regards Jonathan On Mon, 2006-07-10 at 15:51 +1000, Daniel N wrote:> > > On 7/10/06, Jonathan Gill <jonathan.gill@securecirt.com> wrote: > Hi All > > How do I access a model from within the application.rb? > > What im tying to do is pull some data from a model (news in > this case) > that must be displayed on each page within the entire > application. I > know I could drop this into each controller, have each view > pull it from > the controller, but thats repeating myself... > > So whats the best way to do this (as I dont think models are > visible > from within application.rb )? > > application.rb is a controller that all other controllers inherit > from. Models are just as visible in the application controller as any > other. > > There are a couple of ways to do this. Helper methods, before_filters > and a couple of more obscure ones. > > Probably using both would be benificial. You can define the method in > the controller and then tell it to act as a helper method as well. > Then you get it in the controller and the views. > > First, use a before_filter in the application controller > > class ApplicationController < ActionController::Base > > before_filter :set_some_variable > helper_methods :some_variable > > > def some_variable > @variable > end > > protected > def set_some_variable > @variable = MyModel.find(:first) > end > end > > This will provide a method some_variable to both views and > controllers. The @variable will be set every time the controller is > fired up. > > Hope this helps > > Cheers > > > > > > Many thanks > > Jonathan > > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2.2 (GNU/Linux) > > iD8DBQBEseUL/Nj+exW3AocRAm6dAKDGSK5Yv > +s7Ih1aHoBsVo20wM5NMACfQVT8 > JRWQzUXPexrtujy1/NcdeN8> =6K+t > -----END PGP SIGNATURE----- > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Jonathan Gill <jonathan.gill@securecirt.com> -------------- 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/20060710/16faae41/attachment.bin