从 jquery 和 haml 渲染部分内容

发布于 2024-09-13 01:23:47 字数 858 浏览 11 评论 0原文

我计划执行的功能是根据从选择标记中选择的数字插入一些表单元素。

我有一个名为 number_of_passengers 的选择标签,并且我计划为所选数量动态附加新的乘客字段。假设我从 number_of_passengers 中选择 2 个表单,那么 2 个表单应该出现在字段集中。这些表格包含姓名、年龄、体重等。

我尝试按照以下步骤操作:

从 jquery 调用 Rails 函数?

并将其转换为 haml-speak,但每当我使用 :javascript 标记时都会出现错误。另外,我认为一旦我进入其中,我就无法“转义”javascript标签

:javascript
  $('#number_of_passengers').change(function() {
    var $num_of_passengers = $(this).val();
    for($i=0; $i<$num_of_passengers;$i++) {
      $('.passenger-info ul').append('<%= escape_javascript( render :partial => "new_passenger", :locals => {:booking => @booking }) %>');
    }
  })

,因为我处于form_for中,如何将@booking变量传递到本地?这看起来真的很复杂,我计划用肮脏的方式循环 20 次(最多 20 名乘客),然后根据所选的数字隐藏/显示它们。但你不觉得这太脏了吗?

The functionality I plan on doing is to insert some form elements depending on a number chosen from a select tag.

I have a select tag called for number_of_passengers, and i plan to dynamically append new passenger fields for the number chosen. Say I select 2 from number_of_passengers, then 2 forms should appear in a fieldset. these forms contain name, age weight etc.

I tried following this:

call a rails function from jquery?

and just converted it to haml-speak but I get errors whenever I use the :javascript tag. Also I don't think I can "escape" the javascript tag once I am in it

:javascript
  $('#number_of_passengers').change(function() {
    var $num_of_passengers = $(this).val();
    for($i=0; $i<$num_of_passengers;$i++) {
      $('.passenger-info ul').append('<%= escape_javascript( render :partial => "new_passenger", :locals => {:booking => @booking }) %>');
    }
  })

also since I am in a form_for, how do I pass the @booking variable to the local? It seems really complicated and I'm planning of doing the dirty way out of just looping 20 times(20 max passengers) then just hide/show them depending on the selected number. But that's too dirty don't you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

嘿看小鸭子会跑 2024-09-20 01:23:47

要使插值工作,您需要执行

!= "$('.passenger-info ul').append('#{escape_javascript( render :partial => 'new_passenger', :locals => {:booking => @booking })}');

更简单的操作:将 != 添加到行的开头,并将字符串包含在双引号之间。

您关于 @booking 的问题:我建议您研究一些嵌套表单示例,以便让您更清楚(例如 这个

To get the interpolation working you need to do something like

!= "$('.passenger-info ul').append('#{escape_javascript( render :partial => 'new_passenger', :locals => {:booking => @booking })}');

so simpler said: add the != to the start of the line, and include the string between double quotes.

Your question about the @booking: I suggest you investigate some nested forms examples to make it clearer for you (e.g. this and this railscast).

拥抱没勇气 2024-09-20 01:23:47

我实际上通过添加来做到这一点:

respond_to do |format|
  format.js { render :partial => "new_passenger" }
end

所以即使请求是在js中,它也会返回一个html部分。俏皮

i actually managed to do this by adding:

respond_to do |format|
  format.js { render :partial => "new_passenger" }
end

so even if the request was in js, it will return an html partial. Nifty

上课铃就是安魂曲 2024-09-20 01:23:47

Rails 语法一直在变化,但这不应该是这样。

render :partial => "new_passenger", :locals {:booking => @booking })

render :partial => "new_passenger", :locals => {:booking => @booking })

可以在 javascript 块中转义 javascript,因为您正在评估 ruby​​ 标签。

您收到哪些错误消息?

我不清楚你问题的后半部分,但这就是你要找的:

- form_for @user do |f|
  # f.object is equal to @user

Rails syntax changes all the time but shouldn't this

render :partial => "new_passenger", :locals {:booking => @booking })

be

render :partial => "new_passenger", :locals => {:booking => @booking })

You can escape javascript within the javascript block because you're evaluating ruby tags.

Which error messages are you receiving?

The latter part of your question isn't clear to me but is this what you're looking for:

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