Using multiple controllers in one view in Rails

I am working on something like a social network; I use various APIs from various websites, for example. Last.FM, Delicious, Twitter, ...

I created one controller per website (there are currently 7 of them).

View Examples:

localhost:3000/lastfm <- All datas i gathered from user Last.fm account localhost:3000/twitter <- All datas i gathered from user Twitter account ... 

Now I want to show this data in one view (localhost: 3000 / index.hmtl) using these different controllers.

Components are deprecated by creating one controller and burying the entire API, which also seems ugly.

Therefore, I do not know how to do this. Any idea?

+7
ruby ruby-on-rails
source share
3 answers

First of all, you must put all the methods for storing and collecting data in resources and models so that they are accessible from all controllers. However, you can save internal data change operations in your individual controllers. Once you have organized it this way, you can do what Hobo does: create a controller for the first page only, "front_controller" if you want. Here you can display data collected from all your models and resources, as well as links to other actions of your controller.

Here are some interesting thoughts on better organizing your models and controllers (thick models, skinny controllers are a rule of thumb. Since you said you use a different API (like lastfm and twitter), you can take a look at this railscasts about creating models without ActiveRecord ( models that are not database bound)

here is some kind of pseudo code, keep in mind that it really only focuses on your question.

 # pseudo code class TwitterController < ApplicationController def index @services = { :twitter => TwitterModel.find(:all, ...), } end def update_twitter TwitterUpdaterClass.update { |twit| _m = TwitterModel.new _m.message = twit.msg _m.from = twit.from # .. _m.save } end end class MyIndexController < ApplicationController def index @services = { :twitter => TwitterModel.find(:all, ...), :lastfm => LastFmModel.find(:all, ...) } end end 

it might be much better for background employees to update your leisure services, rather than a controller that needs to be called up every time you want to get the latest tweets. here is a good article showing - 6 ways to run background jobs in rubyonrails

  # more pseudo code class TwitterWorker < BackgrounDRb::MetaWorker set_worker_name :twitter_worker def create(args = nil) # instead of TwitterController.update_twitter TwitterUpdaterClass.update { |twit| _m = TwitterModel.new _m.message = twit.msg _m.from = twit.from # .. _m.save } end end 
+8
source share

First of all, you must put all the methods for storing and collecting data in resources and models so that they are accessible from all controllers. However, you can save internal data change operations in your individual controllers. Once you have organized it this way, you can do what Hobo does: create a controller for the first page only, "front_controller" if you want. Here you can display data collected from all your models and resources, as well as links to other actions of your controller.

+6
source share

I think you should read a little bit about rails MVC architecture . It seems to me that you are neglecting the part of M (odel). Models should contain data, which is the most important part of your application.
These are somewhat interesting thoughts about better organizing your models and controllers (thick models, skinny controllers are a rule of thumb.
Since you said you were using a different API (e.g. lastfm and twitter), you can take a look at this railscast about creating non-ActiveRecord models (models that are not database bound)

If you also provide an API for your users, I suggest using the RESTful approach, as it really can be easily developed and maintained as soon as you hang it.
You should learn more about resources , since your localhost/lastfm and localhost/twitter are resources and not .

Hope this helps. Good luck.

+4
source share

All Articles