2013-07-28 74 views
0

使用jQuery酥料饼的CRUD我尝试添加的jQuery酥料饼到轨默认cruds而不是redireting到的意见和什么我做的是呈现形式,jquery的酥料饼:轨通过引导

<%= content_tag_for :tr, @order.order_items do |i| %> 
<a class='btn btn-mini label-with-popover' id=<%=i.id%>><%= t('helpers.links.edit_html') %> </a> 
<% end %>  

<script type="text/javascript"> 
    $(function() { 
     $('.label-with-popover').popover({ 
     html : true, 
     content: "<%= escape_javascript(render :partial => 'form_item') %>" , 
     placement: 'top' 
     }); 
    }); 
    </script> 

,这里是我的form_item:

<%= simple_form_for :order_item, url: admin_shop_order_path, :html => { :class => 'form-horizontal' } do |f| %> 
    <div class='form-inputs'> 
    <%= f.input :item_id , :collection => @shop.products.collect{|b| b.variants}.flatten.map{|variant| ["#{variant_full_title(variant)}", variant.id]}%> 
    <%= f.input :quantity %> 
    </div> 
    <div class="form-actions"> 
    <%= f.button :submit, :class => 'btn-primary' %> 
    <%= link_to t("helpers.links.cancel"), admin_shop_orders_path, :class => 'btn' %> 
    </div> 
<% end %> 

但问题出现在编辑按钮上。为了呈现编辑表单,我们需要我们想要编辑的对象(:order_item我的意思是)以及通过使用id来获取的方法,这就是为什么我设置了锚点标记ID。现在我们必须在popover函数中获取锚点ID,但$(this).id不起作用。任何建议?

回答

1

在jQuery中,您需要使用attr('id')来获取元素的id。尝试更换$(本).ID有:

$(this).attr('id') 

见jQuery的文档了解更多信息:http://api.jquery.com/attr/

但首选的方式来实现,这通常是使用数据属性。将数据从视图传递给一些JS代码是一种干净的方式。你的链接应该是这样的:

$(this).data('item-id') 

jQuery的文档:

<a class='btn btn-mini label-with-popover' data-item-id=<%=i.id%>>...</a> 

而在你的JS文件,你会用得到这个值http://api.jquery.com/data/

+0

是的,但仍有问题。我们不能在erb标签中使用这样的行:“<%=”+“escape_javascript ....(您的代码)”+“%>”。这是行不通的。我们在erb文件中。每个<%=翻译成ruby代码,如果我们使用“\ <\%\ =”,那么它不会作为ruby代码执行。 – Pooya

+0

哪部分不起作用?你能否更新你的问题以包含来自erb的代码? – jibai31

+0

整个提到的代码是在erb中。 – Pooya

1

要渲染错误,我认为,你应该尝试用本地人渲染你的部分。您也可以将局部变量传递给partials,使它们更加强大和灵活。

在你的情况,而呈现在脚本标签的部分form_item,所以你可以写这样的:

<script type="text/javascript"> 
    $(function() { 
     $('.label-with-popover').popover({ 
     html : true, 
     content: "<%= escape_javascript(render :partial => 'form_item', :locals => {order_item: @order_item}) %>" , 
     placement: 'top' 
     }); 
    }); 
    </script> 

,并在你的表格,你将能够访问它想:

<%= form_for(order_item) do %> 

# write your form stuff %> 

<% end %> 

通过这种方式,您可以处理表单的创建或编辑操作。

首先,我建议你通过ID与添加到它的一些文字(我更换了标识与"link_<%= i.id%>") 第二个通话的一个标签的onclick功能:

<%= content_tag_for :tr, @order.order_items do |i| %> 
<a class='btn btn-mini label-with-popover' id="link_<%=i.id%>" ><%= t('helpers.links.edit_html') onclick="javascript:openPopup(this.id)" %> </a> 
<% end %> 

和最后但并非最不重要的,得到ID在你的功能,并使之通过你的部分。

<script type="text/javascript"> 
    function openPopup(a_id) { 
     var id = a_id.split("link_")[1]; 
     $('.label-with-popover').popover({ 
     html : true, 
     content: "<%= escape_javascript(render :partial => 'form_item', locals => {:id => id}) %>" , 
     placement: 'top' 
     }); 
    }); 
    </script> 

我不擅长的JavaScript,但是从我的答案,你会得到什么,我想解释一下你的。我相信,你会找到一个更好的方法来做到这一点,如果你这样做,请在这里发布。希望它会有所帮助。谢谢

+0

是的,我曾考虑局部变量,但我没有在问题中提到它,但仍然存在问题。你想在哪里设置@order_item?有一堆他们,我​​必须从主播获得ids。 – Pooya

+0

我一定会帮助你。给我5分钟。必须查看一次jQuery popover库。 –

+0

我更新了我的答案。 –