對象Task
包含boolean
字段done
。如何在Ajax
更新狀態後刷新頁面?而不是true
- false
顯示值完整 - 不完整?Rails如何藉助Ajax更新頁面
的routes.rb:
resources :tasks do
member do
post 'done'
end
end
控制器:
def done
@task = Task.find(params[:id])
@task.update_attributes(:done => params[:done])
respond_to do |format|
format.js {}
end
end
查看:
....
<td><%= task.done %></td>
<td><%= check_box_tag 'done', task.id , task.done, :class => "task-check", remote: true %></td>
....
<script>
$(".task-check").bind('change', function(){
$.ajax({
url: '/tasks/'+this.value+'/done',
type: 'POST',
data: {"done": this.checked},
});
});
</script>
更新
編輯的腳本
<script>
$(".task-check").bind('change', function(){
$.ajax({
url: '/tasks/'+this.value+'/done',
type: 'POST',
data: {"done": this.checked},
}).done(function(ajax_response){
location.reload();
});
});
</script>
和控制器
def done
@task = Task.find(params[:id]) # make sure you have this
@task.update_attributes(:done => params["done"])
render :json => { data: "Success", is_done: params[:done] }
end
如何,而無需重啓更新網頁內容?
@toddmetheny我更新了我的問題 –
請參閱下面的編輯。 – toddmetheny