如何在同一个 Rails 控制器中使用不同的 RJS 模板?

发布于 2024-07-06 08:18:35 字数 871 浏览 10 评论 0原文

我有一个控制器方法,它返回一个在部分中呈现的下拉列表,但根据部分的使用位置,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别再吹冷风 2024-07-13 08:18:35

我喜欢迈克的回应,但从设计的角度来看,这里需要考虑一些事情:

在我看来,这应该在视图层中 - 如果操作在语义上是相同的,但表示不同,也许有两个不同的 rjs 部分和做类似下面的事情更符合MVC?

if params[:use_alternate]
  render :partial => "case_1.rjs"
else
  render :partial => "case_2.rjs"
end

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?

if params[:use_alternate]
  render :partial => "case_1.rjs"
else
  render :partial => "case_2.rjs"
end
纵情客 2024-07-13 08:18:35

将条件逻辑放在 one rjs 模板中怎么样?

# services.rjs

if @type == "your conditions"
  # your rjs updates
else
  # your other rjs updates
end

这为您提供了一个更干净的控制器,并免除了维护多个 rjs 模板的麻烦。

What about placing the conditional logic in one rjs template?

# services.rjs

if @type == "your conditions"
  # your rjs updates
else
  # your other rjs updates
end

This gives you a cleaner controller and saves you the headache of maintaining multiple rjs templates.

向日葵 2024-07-13 08:18:35

就像是:

if params[:use_alternate]
  render :template => alternate.rjs and return
end

something like:

if params[:use_alternate]
  render :template => alternate.rjs and return
end
陪你到最终 2024-07-13 08:18:35

为了保持简洁,我有两个控制器方法来呈现两个不同的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文