如何在同一个 Rails 控制器中使用不同的 RJS 模板?
我有一个控制器方法,它返回一个在部分中呈现的下拉列表,但根据部分的使用位置,RJS 模板需要不同。 我可以将参数传递给控制器来确定使用哪个 RJS 吗?
这是控制器方法,非常简单:
def services
respond_to do |format|
format.js {
@type = HospitalCriteria.find_by_id(params[:type_id])
@services = @type.children.all
}
end
end
这是自动渲染的 rjs 模板
page.replace_html 'select_service', :partial => 'hospital/services'
page.replace_html 'select_condition', :partial => 'hospital/conditions'
page.replace_html 'select_procedure', :partial => 'hospital/procedures'
page << 'if ($("chosenType") != null) {'
page.replace_html 'chosenType', @type.name
page.replace_html 'chosenService', 'Selected Service'
page.replace_html 'chosenCondition', 'Selected Condition'
page.replace_html 'chosenProcedure', 'Selected Procedure'
page << '}'
I have a controller method that returns a list for a drop down that gets rendered in a partial, but depending on where the partial is being used, the RJS template needs to be different. Can I pass a parameter to the controller that will determine which RJS gets used?
Here is the controller method, it is very simple:
def services
respond_to do |format|
format.js {
@type = HospitalCriteria.find_by_id(params[:type_id])
@services = @type.children.all
}
end
end
And here is the rjs template the gets rendered automatically
page.replace_html 'select_service', :partial => 'hospital/services'
page.replace_html 'select_condition', :partial => 'hospital/conditions'
page.replace_html 'select_procedure', :partial => 'hospital/procedures'
page << 'if ($("chosenType") != null) {'
page.replace_html 'chosenType', @type.name
page.replace_html 'chosenService', 'Selected Service'
page.replace_html 'chosenCondition', 'Selected Condition'
page.replace_html 'chosenProcedure', 'Selected Procedure'
page << '}'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我喜欢迈克的回应,但从设计的角度来看,这里需要考虑一些事情:
在我看来,这应该在视图层中 - 如果操作在语义上是相同的,但表示不同,也许有两个不同的 rjs 部分和做类似下面的事情更符合MVC?
I like Mike's response, but something to think about here from a design perspective:
It sounds to me like this should be in the view layer - if the action is semantically the same, but the presentation is different, perhaps having two different rjs partials and doing something like below is more compliant with MVC?
将条件逻辑放在 one rjs 模板中怎么样?
这为您提供了一个更干净的控制器,并免除了维护多个 rjs 模板的麻烦。
What about placing the conditional logic in one rjs template?
This gives you a cleaner controller and saves you the headache of maintaining multiple rjs templates.
就像是:
something like:
为了保持简洁,我有两个控制器方法来呈现两个不同的 RJS。 然后,我在两个控制器方法调用的公共受保护方法中设置 @type 和 @services。
在我看来,您在每种情况下都要求不同的东西,因此调用不同的控制器方法。 传递一个标志来改变方法的工作方式只是一种黑客行为,当你有 3、4 或 5 个位置时,它不会很好地扩展。 尽管您将生成更多代码,但它会更容易维护。
To keep things clean, I'd have two controller methods that render the two different RJSs. I'd then set @type and @services in a common protected method that the two controller methods call.
In my mind you are asking for something different in each case so call a different controller method. Passing in a flag to change the way the method works is just a hack and won't scale well when you have 3, 4 or 5 places. Even though you'll generate more code, it will be easier to maintain.