如何在 Ruby on Rails 中使用子控制器?
我正在学习 RoR,并且我有一个正在尝试解决的一般设计问题。我想获得一些关于答案的意见,以确保我遵循某种最佳实践。
我有一个由 3 个小节组成的页面,我们称它们为 A、B 和 C。某些操作会导致每个小节通过 AJAX 刷新,所以我想让它们每个都有一个控制器操作,允许它们中的任何一个进行渲染单独,没有页面镶边。
我现在构建的方式是使用一个具有 4 个操作的控制器:index、A、B、C
A、B 和 C 中的每一个都使用layout=> false 渲染其视图,因此我可以通过以下方式渲染该部分:需要时使用 AJAX。然后,你有索引,它会渲染一些额外的东西以及 A、B 和 C 的视图。
本质上我想要的是 3 个子控制器,以及一个根据需要调用子控制器的主控制器。或者我认为这就是我想要的。我认为我不想要的是部分视图,因为我在控制器中为每个 A、B、C 做了一些设置,然后我必须在索引控制器和索引控制器中复制设置代码A、B、C。
我想到的代码是这样的:
my_controller.rb:
class MyController < ApplicationController
def index
@aOutput = A
@bOutput = B
@cOutput = C
render //can use @aOutput, @bOutput, @cOutput in the view
end
def A
render :layout => false
end
def B
render :layout => false
end
def C
render :layout => false
end
end
这将使我现在能够在需要完整页面时访问 MyController#index,以及当我想要重新渲染每个小节的内容时访问 MyController#A 等。
这里的问题是,如果代码的结构是这样的,那么在访问 MyController#index 时就会遇到 DoubleRender 错误。处理这种事情的正确方法是什么?请随意推翻我在这里所做的任何假设,唯一的要求是页面具有三个部分,可以根据需要单独更新,而无需重新加载整个页面。
I am learning RoR, and I have a general design issue that I'm trying to work around. I want to get some input on the answer to make sure I follow some kind of best practice.
I have a page that's made up of 3 subsections, let's call them A, B, and C. Certain actions cause each of these to refresh via AJAX, so I want to have them each have a controller action that allows any of them to render individually, without the page chrome.
The way I've structured this right now is with a controller that has 4 actions: index, A, B, C
Each of A, B, and C renders its view with layout=>false so I can render just that piece via AJAX when needed. Then, you have index, which renders some extra stuff along with the view of A, B, and C.
Esentially what I want here are 3 subcontrollers, and a master controller that invokes the subcontrollers as needed. Or I think that is what I want. What I think I don't want is partial views, because there is some setup I do in the controller for each of A,B,C and I would then have to duplicate the setup code in both the controller for index and the controller for A,B,C.
The code I have in mind is something like this:
my_controller.rb:
class MyController < ApplicationController
def index
@aOutput = A
@bOutput = B
@cOutput = C
render //can use @aOutput, @bOutput, @cOutput in the view
end
def A
render :layout => false
end
def B
render :layout => false
end
def C
render :layout => false
end
end
This would enable me to now access MyController#index when I want the full page, and MyController#A, etc when I want to re-render the contents of each subsection.
The problem here is if the code is structured like this, you will run into DoubleRender errors when accessing MyController#index. What is the correct way to approach this kind of thing? Feel free to blow up any assumptions I made here, with the only requirement being a page that has three sections that can update individually as needed without reloading the whole page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否清楚地理解了这个问题,但我认为这就是我认为你的问题所在。
您希望能够仅渲染视图而不调用整个控制器操作。
这可以通过使用 render :action => 来完成:action_name
您只想为 ajax 调用呈现一些文本
使用
respond_to
块来实现此目的。例如。您提出的设计存在基本缺陷,例如不遵守某些 SOLID 设计原则。我强烈建议您阅读一本关于 Rails 基础知识的好书。
I am not sure if I understand the problem clearly but I think here is what I think your problem is.
You want to be able to render only a view without invoking controller action entire.
This can be done by using
render :action => :action_name
You want to render some text only for ajax calls
Use
respond_to
block in order to achieve this. For example.The design that you are proposing has basic flaws like non-adherence to some of the SOLID design principles. I strongly recommend reading a good book on basics of rails.