从jquery和haml渲染部分 [英] render a partial from jquery and haml

查看:111
本文介绍了从jquery和haml渲染部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算做的功能是根据从选择标记中选择的数字来插入一些表单元素.

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

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

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.

我尝试了以下操作:

从jquery调用Rails函数吗?

并将其转换为haml-speak,但是每当使用:javascript标记时,我都会收到错误消息.另外,我认为一旦进入javascript标签,我就无法转义"

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 }) %>');
    }
  })

同样由于我在form_for中,如何将@booking变量传递给本地?看来确实很复杂,我打算从循环20次(最多容纳20名乘客)中走出来,然后根据所选择的号码隐藏/显示它们,以此为手段.但这太脏了,你不觉得吗?

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?

推荐答案

我实际上通过添加以下内容来做到这一点:

i actually managed to do this by adding:

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

因此,即使请求在js中,它也会返回html局部.漂亮

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

这篇关于从jquery和haml渲染部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆