Rails 如何调用另一个控制器中的操作并获取该操作运行的控制器代码?
可能的重复:
Rails:从控制器调用另一个控制器操作
嗨,我'我是 Rails 新手,在主页视图中我想调用其他控制器中的操作。 我正在使用渲染“the/relevant/url”。
这是调用视图但没有命中正确的控制器操作代码,因此 没有设置视图期望的视图模型。
我来自 asp.net mvc 世界,在这里我们可以使用渲染操作,这将命中该操作的控制器代码。
这可以在 Rails 中完成吗?
如果不能的话,asp.net 执行 mvc 模式是错误的吗?
非常感谢
更新
我的目的是从主页的视图调用另一个控制器操作来获取与主控制器详细信息无关的辅助数据,例如。当天报价或当天热门交易。
Possible Duplicate:
Rails: call another contoller action from a controller
Hi I'm new to rails and in my home page view I want to call actions in other controllers.
I am using render 'the/relevant/url'.
This is calling the view but not hitting the correct controller actions code and therefore
not setting up the view models that the views are expecting.
I am coming from asp.net mvc world and here we can use render action and this will hit the controller code for the action.
Can this be done in rails?
Is asp.net doing mvc pattern wrong if you can't?
Many Thanks
UPDATE
My intention is to call from the home page's view another controller actions to get side data not related to the main controllers details, eg. Quote of the day or Hot deal of the day.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这被认为是不好的做法。当您在控制器中构建视图时,很大一部分数据将是有状态的。使用渲染和模板覆盖可以组合出一个疯狂的解决方案,但基本上,您永远不应该这样做。
相反,使用
redirect_to other_controller_action_url
(默认情况下会发送 HTTP 302 FOUND),或者更好的是,找出代码重复的位置并将其分解到帮助程序或演示程序中。编辑:如果你想在侧边栏中添加一些内容,那么你不应该使用整个 Rails 堆栈来渲染响应的一小部分。相反,您最好使用助手来生成特定数据并使用部分数据来渲染侧边栏。
This is considered bad practice. When you are building the view in the controller, a significant portion of the data will be stateful. It is possible to hack together a crazy solution using
render
and template overrides, but basically, you should never do this.Instead, use
redirect_to other_controller_action_url
(which will send a HTTP 302 FOUND by default), or even better, figure out where your code is repeating and factor it out into a helper or presenter.Edit: if you are trying to get something to stuff in a sidebar like that, you shouldn't be using the entire rails stack to render such a small part of your response. Instead, you would be better off using helpers to generate the specific data and using a partial to render out you sidebar.
您必须使用redirect_to,将一个控制器操作传递到rails 中的另一个控制器操作。
You will have to use redirect_to, to pass from one controller action to another in rails.
“这是调用视图,但没有命中正确的控制器操作代码,因此没有设置视图期望的视图模型。”
很混乱。
我会使用一两本书来阅读更多内容,例如《Agile Web Development with Rails》和 O'Reilly Ruby 书。
从浏览器页面开始,即视图。因此,用户单击链接,它会调用控制器操作,执行一些操作。它可以用Controller.action调用其他控制器的action来做事情。它显示的 html 将来自初始控制器渲染语句,除非它重定向到另一个执行渲染的控制器。
当您来自 asp、php 等时,rails 意味着重新理解您的理解,这通常需要一段时间。我来自冷聚变,必须经历这个过程。
"This is calling the view but not hitting the correct controller actions code and therefore not setting up the view models that the views are expecting."
is very confusing.
I would just read up more using a book or two, like Agile Web Development with Rails and the O'Reilly Ruby book.
Start with the browser page, i.e. view. So a user clicks a link, it calls a controller action, does some stuff. It can call others controllers action with Controller.action to do things. The html it displays will be from the initial controllers render statement, unless it redirects to another controller which does the render.
When you come from asp, php, etc. rails means re-wiring your understanding and it usually takes a while. I came from cold fusion and had to go thru this process.