Forum: Ruby on Rails Getting data from multiple controllers (Newbie question)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
43be2e1b4b5e8d236affc41a03800671?d=identicon&s=25 Fred (Guest)
on 2006-03-30 11:28
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
43be2e1b4b5e8d236affc41a03800671?d=identicon&s=25 Fred (Guest)
on 2006-03-31 09:34
(up, little question which went unseen)
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-03-31 09:47
(Received via mailing list)
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)
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-03-31 11:49
(Received via mailing list)
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
43be2e1b4b5e8d236affc41a03800671?d=identicon&s=25 Fred (Guest)
on 2006-03-31 13:49
> 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)
54077fb03c95861af85880218cf7aef2?d=identicon&s=25 Gokhan Arli (sylow)
on 2006-03-31 13:49
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 Bruun

divide 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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-03-31 14:23
(Received via mailing list)
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)
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-03-31 16:17
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
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2006-03-31 16:38
(Received via mailing list)
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.
43be2e1b4b5e8d236affc41a03800671?d=identicon&s=25 Fred (Guest)
on 2006-04-01 16:32
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/ActionControlle...

Regards,

Fred
This topic is locked and can not be replied to.