Fred
2006-Mar-30 09:28 UTC
[Rails] Getting data from multiple controllers (Newbie question)
Hello, Assuming I have the following setup for one page: <column A><data from controller 1 /> </columnA> <column B><data from controller 2 /> </columnB> <column C><data from controller 3 /> </columnC> Assuming the page is created for Controller 2, what would be the best way(s) to gather/render the data from the other controllers? Best regards and thanks for your insight Fred -- Posted via http://www.ruby-forum.com/.
Fred
2006-Mar-31 07:34 UTC
[Rails] Re: Getting data from multiple controllers (Newbie question)
(up, little question which went unseen) -- Posted via http://www.ruby-forum.com/.
Mikkel Bruun
2006-Mar-31 07:45 UTC
[Rails] Re: Getting data from multiple controllers (Newbie question)
You really cant have data from several controllers in one view... it seems like you need the study the mvc pattern, and how it is applied in rails. On Friday, March 31, 2006, at 9:34 AM, Fred wrote:>(up, little question which went unseen) > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsMikkel Bruun www.strongside.dk - Football Portal(DK) nflfeed.helenius.org - Football News(DK) ting.minline.dk - Buy Old Stuff!(DK) -- Posted with http://DevLists.com. Sign up and save your time!
njmacinnes@gmail.com
2006-Mar-31 09:46 UTC
[Rails] Re: Getting data from multiple controllers (Newbie question)
If you really do want to do it for some reason, then you could look into Ajax, but since you are a self proclaimed newbie, it''s probably not worth the effort, and I expect there''s a far easier way of doing whatever it is you want to do. -Nathan On 31 Mar 2006 07:45:31 -0000, Mikkel Bruun <devlists-rubyonrails@devlists.com> wrote:> You really cant have data from several controllers in one view... > > it seems like you need the study the mvc pattern, and how it is applied > in rails. > > On Friday, March 31, 2006, at 9:34 AM, Fred wrote: > >(up, little question which went unseen) > > > >-- > >Posted via http://www.ruby-forum.com/. > >_______________________________________________ > >Rails mailing list > >Rails@lists.rubyonrails.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > Mikkel Bruun > > www.strongside.dk - Football Portal(DK) > nflfeed.helenius.org - Football News(DK) > ting.minline.dk - Buy Old Stuff!(DK) > > > > > -- > Posted with http://DevLists.com. Sign up and save your time! > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Gokhan Arli
2006-Mar-31 11:50 UTC
[Rails] Re: Re: Getting data from multiple controllers (Newbie quest
unknown wrote:> If you really do want to do it for some reason, then you could look > into Ajax, but since you are a self proclaimed newbie, it''s probably > not worth the effort, and I expect there''s a far easier way of doing > whatever it is you want to do. > -Nathan > > > On 31 Mar 2006 07:45:31 -0000, Mikkel Bruundivide every piece in views into partials and all.rhtml <%= render :partial => "a" %> <%= render :partial => "b" %> <%= render :partial => "c" %> Offcourse you need to get data from controllers but you can move the up the functions to base controller and call them in your new controller Gokhan Arli www.sylow.net -- Posted via http://www.ruby-forum.com/.
Fred
2006-Mar-31 11:52 UTC
[Rails] Re: Getting data from multiple controllers (Newbie question)
> You really cant have data from several controllers in one view... > > it seems like you need the study the mvc pattern, and how it is applied > in rails.For instance, let''s assume I''d like to have statistics regarding usage in div1, an rss feed in div3, some other data in div4, and then my real action result in div2, what would be the best way to do this? I think I understand the MVC design pattern, but still I tend to think that one web page is not limited to viewing one aspect. Maybe I''m wrong, but take 43Things.com, for instance. The main page is displaying some tags, but also a list of top cities on the side column. Roughly, this is what I''d like to do, in a clean and efficient way (components, apparently, are strongly advised against, even in the official documentation) -- Posted via http://www.ruby-forum.com/.
Mikkel Bruun
2006-Mar-31 12:20 UTC
[Rails] Re: Getting data from multiple controllers (Newbie question)
if you dont want to use components, * you can access the models directly from the view. This is controversial at best. But imho calling Class methods on models in the view is OK! News.latest would be fine with me... * Load all the secondary models in your action. Either explicit in the action, or implicit in a filter... Both methods works ok regarding "secondary models"...>(components, apparently, are strongly advised against, even in the >official documentation)Mikkel Bruun www.strongside.dk - Football Portal(DK) ting.minline.dk - Buy Old Stuff!(DK) -- Posted with http://DevLists.com. Sign up and save your time!
Jeff Coleman
2006-Mar-31 14:17 UTC
[Rails] Re: Getting data from multiple controllers (Newbie question)
Fred wrote:> For instance, let''s assume I''d like to have statistics regarding usage > in div1, an rss feed in div3, some other data in div4, and then my real > action result in div2, what would be the best way to do this?There are several aspects to this question. The main ones to look at are the View and the Controller. A Controller''s action is just the routine which provides the business logic for the View. If you want to have statistics, RSS feed, and other data in a single View, then the bottom-line simplest way to do it is to call all that business logic in the single action. Then you put all the display logic in the various DIVs located in your single View. To make this more DRY, you can abstract the specific parts of the business logic into different methods, either controller methods ,helper methods or components. You can also abstract the View into multiple partial templates. So in your controller''s "view" directory, you might have _statistics.rhtml, _rss.rhtml, and _otherdata.rhtml. Then you could make either a helper or a controller method called "statistics" and "rss", and call them from your main action: data_controller.rb: class DataController < ApplicationController # whatever yours is called def index # again, whatever specific controller you call to show the page @statistics = self.generate_statistics() @rss_feeds = self.generate_rss() end ... other methods, actions, etc end Then in your View you might call the partials: index.rhtml: <div id="statistics"> <h1>Today''s statistics:</h1> <% render :partial => "statistics" %> </div> <div id="rss"> <h1>Your feeds:</h1> <% render :partial => "rss_feeds" %> </div> And since you have an instance variable called @statistics, it''s sent to the partial of the same name as a local variable. So within your partial "statistics", you could do this: _statistics.rhtml: <p>Total number of entries: <%= statistics.total_entries %></p> <p>Total logins: <%= statistics.total_logins %></p> <p>Most recent login: <%= statistics.most_recent_login %></p> ... etc. If "statistics" isn''t a simple object but you''ve got a few variables you need to refer to in your partial, you can call them by sending the :locals parameter (assuming these instance variables are set in your original controller): (in index.rhtml:) render :partial => "statistics", :locals => { :total_entries => @total_entries, :total_logins => @total_logins, :most_recent_login => @most_recent_login } It''s the controller''s job to assemble all the data you need for your view, regardless of which models or other sources that data comes from. So it''s not a question of putting "data from multiple controllers" into your view, it''s a question of making sure the controller in question gets the data together where you can use it.> (components, apparently, are strongly advised against, even in the > official documentation)Are they? By whom? Just curious, because I''ve used them seldom myself but hadn''t noticed this advice personally. Hope this helps! Jeff -- Posted via http://www.ruby-forum.com/.
Julian Leviston
2006-Mar-31 14:35 UTC
[Rails] Re: Getting data from multiple controllers (Newbie question)
Woudln''t it be best if you could write some controller actions that were private and "grabbed" the data you wanted, and then the "munged" controller action (ie the one which has all the bits uses all threee.. so u have def munged act_1 act_2 act_3 munged_specific_code end def act_1 stuff end ... etc That''s very DRY, right? Julian. On 01/04/2006, at 1:17 AM, Jeff Coleman wrote:> Fred wrote: >> For instance, let''s assume I''d like to have statistics regarding >> usage >> in div1, an rss feed in div3, some other data in div4, and then my >> real >> action result in div2, what would be the best way to do this? > > There are several aspects to this question. The main ones to look at > are the View and the Controller. > > A Controller''s action is just the routine which provides the business > logic for the View. If you want to have statistics, RSS feed, and > other > data in a single View, then the bottom-line simplest way to do it > is to > call all that business logic in the single action. Then you put > all the > display logic in the various DIVs located in your single View. > > To make this more DRY, you can abstract the specific parts of the > business logic into different methods, either controller > methods ,helper > methods or components. You can also abstract the View into multiple > partial templates. > > So in your controller''s "view" directory, you might have > _statistics.rhtml, _rss.rhtml, and _otherdata.rhtml. > > Then you could make either a helper or a controller method called > "statistics" and "rss", and call them from your main action: > > data_controller.rb: > > class DataController < ApplicationController # whatever yours is > called > def index # again, whatever specific controller you call to show > the > page > @statistics = self.generate_statistics() > @rss_feeds = self.generate_rss() > end > ... other methods, actions, etc > end > > Then in your View you might call the partials: > > index.rhtml: > > <div id="statistics"> > <h1>Today''s statistics:</h1> > <% render :partial => "statistics" %> > </div> > > <div id="rss"> > <h1>Your feeds:</h1> > <% render :partial => "rss_feeds" %> > </div> > > And since you have an instance variable called @statistics, it''s > sent to > the partial of the same name as a local variable. So within your > partial "statistics", you could do this: > > _statistics.rhtml: > > <p>Total number of entries: <%= statistics.total_entries %></p> > <p>Total logins: <%= statistics.total_logins %></p> > <p>Most recent login: <%= statistics.most_recent_login %></p> > ... etc. > > If "statistics" isn''t a simple object but you''ve got a few > variables you > need to refer to in your partial, you can call them by sending the > :locals parameter (assuming these instance variables are set in your > original controller): > > (in index.rhtml:) > render :partial => "statistics", :locals => { > :total_entries => @total_entries, > :total_logins => @total_logins, > :most_recent_login => @most_recent_login } > > It''s the controller''s job to assemble all the data you need for your > view, regardless of which models or other sources that data comes > from. > So it''s not a question of putting "data from multiple controllers" > into > your view, it''s a question of making sure the controller in question > gets the data together where you can use it. > >> (components, apparently, are strongly advised against, even in the >> official documentation) > > Are they? By whom? Just curious, because I''ve used them seldom myself > but hadn''t noticed this advice personally. > > Hope this helps! > > Jeff > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Fred
2006-Apr-01 14:32 UTC
[Rails] Re: Getting data from multiple controllers (Newbie question)
I will have a deeper look at your advices, thanks>> (components, apparently, are strongly advised against, even in the >> official documentation) > > Are they? By whom? Just curious, because I''ve used them seldom myself > but hadn''t noticed this advice personally.[quote]Components should be used with care. They?re significantly slower than simply splitting reusable parts into partials and conceptually more complicated. Don?t use components as a way of separating concerns inside a single application. Instead, reserve components to those rare cases where you truly have reusable view and controller elements that can be employed across many applications at once.[/quote] This excerpt comes from http://api.rubyonrails.com/classes/ActionController/Components.html Regards, Fred -- Posted via http://www.ruby-forum.com/.