2013-07-28 76 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

我更新了我的答案。 –