1

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.

4

3 回答 3

2

这被认为是不好的做法。当您在控制器中构建视图时,很大一部分数据将是有状态的。可以使用模板覆盖来拼凑一个疯狂的解决方案render,但基本上,你不应该这样做。

相反,使用redirect_to other_controller_action_url(默认情况下会发送 HTTP 302 FOUND),或者更好的是,找出你的代码在哪里重复,并将其分解为帮助程序或演示程序。

编辑:如果你想在这样的侧边栏中添加一些东西,你不应该使用整个 rails 堆栈来呈现你响应的一小部分。相反,您最好使用帮助程序来生成特定数据并使用部分来呈现您的侧边栏。

于 2012-01-08T11:47:47.993 回答
1

您将不得不使用redirect_to,在rails 中从一个控制器动作传递到另一个控制器动作。

于 2012-01-08T11:37:20.657 回答
1

“这是调用视图,但没有点击正确的控制器操作代码,因此没有设置视图所期望的视图模型。”

很混乱。

我会使用一两本书来阅读更多内容,例如使用 Rails 进行敏捷 Web 开发和 O'Reilly Ruby 书。

从浏览器页面开始,即视图。所以用户点击一个链接,它调用一个控制器动作,做一些事情。它可以用 Controller.action 调用其他控制器的动作来做事。它显示的 html 将来自初始控制器渲染语句,除非它重定向到另一个执行渲染的控制器。

当您来自 asp、php 等时,rails 意味着重新理解您的理解,这通常需要一段时间。我来自冷聚变,不得不经历这个过程。

于 2012-01-08T11:51:09.887 回答