使用复选框,AJAX,jQuery更改属性

我希望更改我的Task资源的布尔属性,具体取决于是否选中了wether复选框。 我被困住了,因为我不知道该做什么……在添加这个AJAX复选框工作,整个CRUD,用rspec和capybara和bootstrapped测试的所有内容之前,我已经拥有了所有内容。 我有以下代码……

意见/任务/ show.html.erb

27  true do |f| %> 28  29  30  

这个:completed是Task的布尔属性。

Java脚本/ tasks.js.coffee

  8 jQuery -> 9 $('#task_completed').bind 'change', (event) => 10 $.post() 

不知道如何完成这个$ .post()的事情,以及如何在控制器中使代码工作……到目前为止我只有这个……

控制器/ tasks_controller.rb

  1 class TasksController  [:show, :edit, :update] 6 7 def show 8 @title = @task.name 9 end 10 11 def new 12 @title = 'New task' 13 @task = @project.tasks.build 14 end 15 16 def create 17 @task = @project.tasks.build(params[:task]) 18 if @task.save 19 current_user.tasks.push(@task) 20 redirect_to [@project, @task], :notice => 'Task created' 21 else 22 render 'new' 23 flash.now[:alert] = 'Task not created' 24 end 25 end 26 27 def edit 28 @title = 'Edit task' 29 end 30 31 def update 32 if @task.update_attributes(params[:task]) 33 redirect_to [@project, @task], :notice => 'Task updated' 34 else 35 render 'edit' 36 flash.now[:alert] = 'Task not updated' 37 end 38 end 39 40 private 41 #controller doesn't respond to these methods as actions 42 43 def find_project_from_project_id 44 @project = current_user.projects.find(params[:project_id]) 45 rescue ActiveRecord::RecordNotFound 46 redirect_to projects_path 47 flash[:alert] = 'Project you were looking for could not be found' 48 end 49 50 def find_task 51 @task = @project.tasks.find(params[:id]) 52 rescue ActiveRecord::RecordNotFound 53 redirect_to project_path(@project) 54 flash[:alert] = 'Task you were looking for could not be found' 55 end 56 57 end 

另外,对于那些想要更多的人……如何为这件事写测试? 我应该为此写它们吗?

编辑:经过研究,我发现人们正在这样做……这是要走的路吗? 在检查元素时,我可以看到当我更改复选框时正在发出新请求,但布尔值仍然是假的…

  8 jQuery -> 9 $('#task_completed').bind 'change', (event) => 10 $('#task_completed').parents('form:first').submit() 11 $('.task_headline').toggleClass('completed_task') 

这在很多方面都不是一个优雅的解决方案,但它的工作原理:)

show.html.erb

  

tasks.coffee

 window.monitorTaskCompletion = (project_id, task_id) -> jQuery -> $('#task_completed').bind 'change', (event) => $.ajax url: "/projects/#{project_id}/tasks/#{task_id}/complete" data: {completed: if $('#task_completed').is(':checked') then 1 else 0} type: "PUT" 

tasks_controller.rb

 def complete @completed = params[:completed].to_i > 0 @task.complete(@completed) respond_to :js end 

您没有为$ .post操作提供属性。

尝试做这样的事情:

 $('#task_completed').bind('change', function() { var url = $(this).closest('form').attr('action'); var data = {}; data.task = {}; data.task.completed = $(this).is(':checked'); $.post({ url: url, data: data }); }); 

当您在IRC会议室解释我时,这将提供您所需的function。 由于复选框值仅在选中复选框时发送,我们必须设置自定义布尔值,无论是否选中复选框(通过$(this).is(’:checked’)函数实现)。

使用Firebug或Chrome / Opera / Safari检查器检查了ajax请求。 您可以在$ .post函数中添加更多function,就像完成事件的函数一样。 查看有关ajax和$ .post函数的jQuery文档:

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.post/

由于这是一个ajax动作,你需要在控制器中以不同的方式处理它。

 def update if @task.update_attributes(params[:task]) @notice = 'Task updated' else @notice = 'Task not updated' end end 

在update.js.erb视图中,类似于:

 $(#notice).html('<%= @notice %>') 

在jquery .post()中,您还应该指定url并包含data参数,如前所述。