我想实现简单的功能。
当我点击复选框
时<%=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 :(得分: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"));
}
});
答案 1 :(得分: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 %>
on my_partial.js
$('.element').html("<%= escape_javascript render(:partial => 'my_partial', :locals => { :if_any => @if_any}) %>");