ruby on rails - Rails3 Update Boolean Checkbox from Index View -



ruby on rails - Rails3 Update Boolean Checkbox from Index View -

i'm building simple tasks application our company part of ordering system.

i have list of tasks number of rules. nil complex... i'm stuck on add-on of checkbox finish task. want done live, index view without having nail submit..

am not sure look. figure need utilize ajax - can recommend tutorial or tell me should looking for.

have thought plugin, edit in place ones out there.

thanks in advance

--- edit 1 --

following advice @pcg79 below, i've added next application not understanding how go out changing status.

in index view have this:

<%= check_box_tag 'complete_task_1', '', false, { 'data-href' => tasks_path(@task) } %><

i've added next application.js (added # phone call properly)

$('#complete_task_1').click(function() { $.ajax({ url: $(this).data('href'), type: 'put', datatype: 'html', success: function(data, textstatus, jqxhr) { // here set flash msg } }); });

for lack of understanding, added tasks controller:

def completed @task = task.find(params[:id]) @task.status = true end

which seemed reasonable wasn't sure how phone call in ajax?

in development log can see sort of working says this:

actioncontroller::routingerror (no route matches "/tasks"):

-- edit 2 --

as per advice @jdc below, i've tried adding next routes.rb:

get 'tasks/:id/completed' => 'tasks#completed', :as => :completed_task

but still routingerror.

-- slight update --

following first-class advise @pcg79 below, i've updated files following.

routes.rb

get 'task/:id' => 'tasks#completed', :as => :completed_task

index.html.erb

<td><%= check_box_tag 'complete_task_1', '', false, { 'data-href' => completed_task_path(:id => task.id) } %></td>

tasks controller

def completed @task = task.find(params[:id]) @task.status = true @task.save end

i no errors in browser, development log shows this:

actioncontroller::routingerror (no route matches "/tasks"):

for simple checkbox, hard work!!!

-- update --

having played day, decided see happen button_to instead, forgetting ajax side of things. set in code:

<%= button_to "complete", completed_task_path(task.id) %>

and changed routes to:

match 'tasks/:id/completed' => 'tasks#completed', :as => :completed_task

which worked treat. changing check_box_tag breaks 1 time again :(

pretty much worked out it's contents of function. having removed code, can update css #:

$('#complete_task_1').click(function() { $.ajax({ success: function(data, textstatus, jqxhr) { $('#thing').css("color","red"); } }); });

any thought i'd need phone call action?? j

if understand you're looking (when checkbox checked or unchecked ajax request sent server , associated object saved result of checkbox), yes you'll want in ajax.

with rails 3 you're using jquery (or, imo, should be). you'll need implement click event on checkbox element. click event, when it's fired, ajax phone call server. you'll want set request since it's update. you'll send id of object , value of checkbox.

there decent amount of sites have examples of rails , ajax. 1 (http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/) has utilize html 5 "data" fields like. there's bunch of similar questions here on so. here's 1 that's not rails give thought of how write jquery (ajax checkboxes).

edit reply question in comment

the checkbox can wherever want since you're doing ajax. if want on index view, that's set it. checkbox (please understand i'm not double checking syntax or anything):

= check_box_tag 'complete_task_1', '', false, { 'data-href' => task_path(@task_1) }

then jquery like:

$('complete_task_1').click(function() { $.ajax({ url: $(this).data('href'), type: 'put', datatype: 'html', success: function(data, textstatus, jqxhr) { // here set flash msg } }); });

second edit: realized forgot send value of checkbox in ajax. can , phone call @task.update_attributes or can create url specific method completes tasks.

edit updated question:

to explain sec edit, in order update task completed, can 1 of 2 things. can either phone call method expressly setting status attribute. or can phone call normal, restful update method passing in :task => {:status => true} , phone call @task.update_attributes(params[:task]). you've chosen former which, imo, fine.

so have 2 problems. first aren't referencing new route points completed method. sec aren't saving object in completed method.

to prepare first problem, need alter path data-href attribute in check_box_tag method points to. don't want task_path. iirc, you'll want completed_task_path(@task). easiest way find out name of path run rake routes in rails project's root directory.

to prepare sec problem, create sure phone call @task.save @ end.

def completed @task = task.find(params[:id]) @task.status = true @task.save end

ruby-on-rails ruby-on-rails-3

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -