2015-10-30 260 views
2

我想實現簡單的功能。Rails ajax渲染部分

當我點擊複選框

<%=check_box_tag :important_only,nil,false,id:'important-notes-only'%> 

數據,鑑於應換新

我的控制器動作

def important_notes 
    deal = Deal.find(params[:id]) 
    @notes = Note.where('is_important = ? and source_id = ?', true, deal.id) 
    p @notes 
    respond_to do |format| 
     format.html { } 
     format.js 
    end 
    end 

路線

 get 'important/:id' => 'notes#important_notes', :as => 'important' 

important.js.erb

should render my partial on view after click 

怎麼辦?

我的AJAX

$(document).ready(function(){ 
$('#important-notes-only').click(function() { 
    if ($('#important-notes-only').is(':checked')) { 
     console.log('clicked') 
     $.ajax({ 
      type: "GET", 
      url: "<%= important_notes_path(@deal.id) %>", 

      success: render partial 
      } 
     }) 

    } 
}); 

}); 如何呈現局部成功

回答

0

您可以使用以下方法:

#app/views/notes/important_notes.js.erb 
$(".element").html("<%=j render 'partial' %>"); 

這是假設你的其他代碼是正確的。在您的更新


看,我會用以下內容:

#view 
<%= check_box_tag :important_only, nil, false, id:'important-notes-only', data: { note: @deal.id } %> 

#app/assets/javascripts/application.js 
$(document).on("click", "#important-notes-only", function(e){ 
    if ($(this).is(':checked')) { 
     $.get("important/" + $(this).attr("data-note")); 
    } 
}); 
0
def important_notes 
    deal = Deal.find(params[:id]) 
    @notes = Note.where('is_important = ? and source_id = ?', true, deal.id) 
    p @notes 
    respond_to do |format| 
     format.html { } 
     format.js{ render 'my_partial', :locals => { :if_any => @if_any } } 
    end 
end 

上查看

<%= check_box '/important/:id' , remote: true do %> 

上my_partial.js

$('.element').html("<%= escape_javascript render(:partial => 'my_partial', :locals => { :if_any => @if_any}) %>");