在Rails中将局部视图从一个控制器的视图渲染到另一个控制器的视图 [英] Rendering Partials From One Controller’s View to Another Controller’s View in Rails

查看:98
本文介绍了在Rails中将局部视图从一个控制器的视图渲染到另一个控制器的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 show的控制器的视图。在该视图内部,我想呈现另一个控制器视图的内容-显然,该视图上的表单也可以与其所属的控制器进行对话的逻辑。我该怎么做呢?我对Rails还是很陌生,我对框架还没有100%的信心。

I have a view for a controller called "show". Inside that view, i want to render the contents of another controller's view - and obviously, the logic for the form on that view to talk to the controller it belongs too. How do I do this? I am fairly new to rails and I'm not 100% confident with the framework yet.

您几乎可以认为它们是视图中的部件。我知道您可以使用以下方法在视图上的同一控制器上呈现动作:

You could almost consider them "widgets" on the view. I know you can render actions from the same controller on the view by using:

render :action => "show_home_page", :layout=> false

但是我需要它来呈现来自另一个控制器的动作(视图)。

but I need it to render the action (view) from another controller.

即。


我有一个视图,即将
渲染到布局上。该视图
属于控制器A。
我需要在原始视图内(上方)呈现控制器B的索引视图和新
视图。

I have a view, which is rendered onto a layout. That view, belongs to controller A. I need to render the "index" and "new" views from controller B inside the original view (above)

推荐答案

问题中的术语有些混乱。如果在控制器中,并且您想在另一个控制器中的另一个操作方法中执行代码并呈现其模板,则应将redirect_to到该操作。假设另一个控制器称为ContractsController

The terminology in your question is a little confused. If are in a controller and you want to execute the code in another action method in another controller and render its template, you should redirect_to that action. Let's say the other controller is called ContractsController

redirect_to :controller => "contracts", :action => "show_home_page"

如果您只想使用另一种方法的视图模板作为操作的响应,您只需要在render参数中添加控制器名称的前缀即可。这不会调用操作,它只会使用其模板。

If you just want to use the view template from another method as the response from your action, you just need to prefix the name of the controller in the render parameter. This will not call the action, it will just use its template.

例如,如果模板位于合同控制器的文件夹中。

For example, if the template lives in the folder for the contracts controller.

render :action => "/contracts/show_home_page", :layout=> false

我认为在这种情况下,您实际上是在谈论部分内容,看起来像

I think in this case you are actually talking about a partial, which would look like

render :partial => "/contracts/show_home_page"

但是,我看到的是您理解的这实际上是您要调用多个操作方法来呈现单个页面。这不是它的工作方式。您将必须设置模板将在单个操作中引用的对象。这是大多数Rails开发人员在模型中投入大量代码的原因之一,因此不会在所有控制器上重复进行安装。

However, what I see you grasping at here is that you actually want to call multiple action methods to render a single page. This is not how it works. You are going to have to set up the objects that the templates will reference in a single action. This is one reason most Rails developers put a lot of code in the models, so the setup isn't repeated all over the controllers.

方式... 引入 JavaScript 的魔力。

在您的页面中,创建一个如下所示的函数:

In your page, create a function like this:

<script type="text/javascript" language="javascript">    
function load_categories() {
            <%= remote_function(:url => {:controller => "categories", :action => "list"},
                                  :update => "categories")%>
        };
</script>

如果您在页面的onload中调用它,它将div替换为id categories 以及所引用操作的响应。

If you call that in the onload even of your page, it will replace the div with id "categories" with the response from the action referenced.

这篇关于在Rails中将局部视图从一个控制器的视图渲染到另一个控制器的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆