我想实现简单的功能。
当我点击复选框时
<%=check_box_tag :important_only,nil,false,id:'important-notes-only'%>
应更新 View 中的数据
我的 Controller 操作
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
}
})
}
});
}); 如何在成功时渲染部分内容
最佳答案
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}) %>");
关于javascript - Rails ajax 渲染部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434605/