Getting data from multiple controllers (Newbie question)


#1

Hello,

Assuming I have the following setup for one page:

<data from controller 1 />
<data from controller 2 />
<data from controller 3 />

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


#2

(up, little question which went unseen)


#3

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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Mikkel B.

www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)


#4

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 B.


#5

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)


#6

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 B.

www.strongside.dk - Football Portal(DK)
ting.minline.dk - Buy Old Stuff!(DK)


#7

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 B.

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 A.
www.sylow.net


#8

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:

Today's statistics:

<% render :partial => "statistics" %>

Your feeds:

<% render :partial => "rss_feeds" %>

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:

Total number of entries: <%= statistics.total_entries %>

Total logins: <%= statistics.total_logins %>

Most recent login: <%= statistics.most_recent_login %>

... 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


#9

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.


#10

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