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

查看:27
本文介绍了在 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)

.

推荐答案

您问题中的术语有点混乱.如果在控制器中,并且您想在另一个控制器中的另一个操作方法中执行代码并呈现其模板,则应该将重定向_到该操作.假设另一个控制器称为 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"

如果您只想使用来自另一个方法的视图模板作为您操作的响应,您只需要在渲染参数中添加控制器名称的前缀.这不会调用操作,它只会使用其模板.

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 中调用它,它将用 id 为categories"的 div 替换为所引用操作的响应.

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天全站免登陆