在 Rails 的一个视图中使用多个控制器 [英] Using multiple controllers in one view in Rails
问题描述
我正在研究诸如社交网络之类的东西;我正在使用来自各种网站的不同 API,例如Last.FM, Delicious, Twitter, ...
I'm working on something like a social networking mesh; I am using different API's from various websites, e.g. Last.FM, Delicious, Twitter, ...
我为每个网站创建了一个控制器(目前有 7 个).
I've created one controller per each website (currently, there are 7).
示例视图:
localhost:3000/lastfm <- All datas i gathered from user's Last.fm account
localhost:3000/twitter <- All datas i gathered from user's Twitter account
...
现在我想通过使用这些不同的控制器在一个视图 (localhost:3000/index.hmtl) 中显示这些数据.
Now I want to show these datas in one view (localhost:3000/index.hmtl) by using these different controllers.
不推荐使用组件,创建一个控制器并将所有 API 埋在其中,这看起来也很丑陋..
Components are deprecated, creating one controller and bury all the API's in that seems ugly, too..
所以我不知道该怎么做.有什么想法吗?
So I don't know how to do this. Any idea?
推荐答案
首先,您应该将所有数据存储和数据收集方法放入资源和模型中,以便所有控制器都可以访问它们.不过,您可以在各个控制器中保留内部数据变异操作.一旦你像这样组织起来,你就可以做 Hobo 所做的:为首页创建一个控制器,如果你愿意的话,front_controller".您可以在此处显示从所有模型和资源收集的数据,以及指向其他控制器操作的链接.
First off, you should put all of the data-storing and data-gathering methods into Resources and Models so they are accessible from all controllers. You can keep the internal data-mutating operations in your individual controllers though. Once you have it organized like this, you could do what Hobo does: create a controller just for the front page, "front_controller" if you will. Here you can display data gathered from all of your models and resources, as well as links to your other controller actions.
这些是一些有趣 关于更好地组织模型和控制器的想法(胖模型、瘦控制器是经验法则.由于您说您正在使用其他 API(如 lastfm 和 twitter),您可能想看看 这个 railscasts 关于创建非 ActiveRecord 模型(不绑定到数据库的模型)
These are a few interesting thoughts on better organizing your models and controllers (fat models, skinny controllers is a rule of thumb. Since you said you are using other API's (like lastfm and twitter), you might want to take a look at this railscasts about creating non ActiveRecord models (models that are not tied to a database)
这是一些伪代码,请记住,它实际上仅针对您的问题.
here is some pseudo code, keep in mind its really only targeted at 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
可能会更好让后台工作人员更新您的休息服务,而不是每次您想要获取最近的推文时都需要调用的控制器.这里有很好的文章展示 - 在 rubyonrails 中运行后台作业的 6 种方法
it might be much better to have background-workers update your rest-services instead a controller which you need to invoke every time you want to fetch recent tweets. here is nice 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
这篇关于在 Rails 的一个视图中使用多个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!