Rails 使用复选框和 jquery ajax 更改布尔值 [英] Rails change boolean value with checkbox and jquery ajax
问题描述
我是 Rails 的新手,并试图通过复选框和使用 jquery ajax 来更改布尔值:
<%- @tasks.each do |task|%><div class="task-wrapper"><%= check_box_tag '已完成', task.id , task.completed, :class =>任务检查"%><%= content_tag :span, task.task %><%= content_tag :span, task.deadline %>
<%结束%>
和 javascript:
$(".task-check").bind('change', function(){如果(this.checked){var bool = this.checked ?1:0;$.ajax({url: '/todos/toggle',类型:'POST',数据:'{"task_id":"'+ this.value +'", "bool":"'+ bool +'"}'});}别的 {警报(否");}});
然后是控制器:
def 切换(task_id, bool)@task = Todo.find_by_id(task_id)如果@task != nil?@task.update_attributes(:completed => bool)别的set_flash "错误,请重试"结尾结尾
最后是路线:
资源:待办事项做会员做发布切换"结尾结尾
<块引用>
也尝试了集合,但给出了同样的错误.
每当我尝试它时,我都会收到 404 错误
.
有什么问题?
谢谢
尝试以下操作(其他一切保持原样):
javascript:
$(".task-check").bind('change', function(){如果(this.checked){$.ajax({url: '/todos/'+this.value+'/toggle',类型:'POST',数据:{完成":this.checked}});}别的 {警报(否");}});
控制器:
def 切换@task = Todo.find(params[:id])如果@task.update_attributes(:completed => params[:completed])# ... 更新成功别的# ... 更新失败结尾结尾
查看 bundle exec rake routes
以向您展示 rails 生成的路径.如果您的 post 'toggle'
是成员,您将获得类似 /todos/:id/toggle
的路径,因此在 ajax 中更新了 url.>
在控制器中,路径中的 :id
以 params[:id]
结尾.来自 ajax 请求的数据也以 params
哈希结束,因此 params[:completed]
.
I am new to rails and trying to change the value of a boolean via a checkbox and using jquery ajax:
<%- @tasks.each do |task| %>
<div class="task-wrapper">
<%= check_box_tag 'completed', task.id , task.completed, :class => "task-check" %>
<%= content_tag :span, task.task %>
<%= content_tag :span, task.deadline %>
</div>
<% end %>
and the javascript:
$(".task-check").bind('change', function(){
if (this.checked){
var bool = this.checked ? 1 : 0;
$.ajax({
url: '/todos/toggle',
type: 'POST',
data: '{"task_id":"'+ this.value +'", "bool":"'+ bool +'"}'
});
}
else {
alert("no");
}
});
then the controller:
def toggle(task_id, bool)
@task = Todo.find_by_id(task_id)
if @task != nil?
@task.update_attributes(:completed => bool)
else
set_flash "Error, please try again"
end
end
finally the routes:
resources :todos do
member do
post 'toggle'
end
end
also tried collection but gives the same error.
when ever i try it i get a 404 error
on the action.
what is the problem?
thanks
Try the following (leaving everything else as is):
the javascript:
$(".task-check").bind('change', function(){
if (this.checked){
$.ajax({
url: '/todos/'+this.value+'/toggle',
type: 'POST',
data: {"completed": this.checked}
});
}
else {
alert("no");
}
});
the controller:
def toggle
@task = Todo.find(params[:id])
if @task.update_attributes(:completed => params[:completed])
# ... update successful
else
# ... update failed
end
end
Take a look at bundle exec rake routes
to show you the paths that rails generates. In the case of your post 'toggle'
which is a member you get a path like /todos/:id/toggle
, hence the updated url in the ajax.
In the controller the :id
from the path ends up in params[:id]
. The data from the ajax request also ends up in the params
hash, hence params[:completed]
.
这篇关于Rails 使用复选框和 jquery ajax 更改布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!