javascript - Implementing AJAX call on checkbox in Rails to update db -
rails newbie here.
i trying make checkbox called "active" act form when checkbox checked/unchecked , use ajax automatically update database changed attribute (without submit button). using instructions here: http://trevorturk.com/2010/08/24/easy-ajax-forms-with-rails-3-and-jquery/ can't seem make work correctly.
in view:
<%= form_for @post, :remote => true |f| %> <%= f.label :active %> <%= f.check_box :active, :class => 'submittable' %> <% end %>
in posts_controller.rb
def update if @post.update(post_params) flash[:notice] = "your post edited." redirect_to post_path(@post) else render :edit end end
then made file called 'archive.js.erb' in views/posts folder code:
$('.submittable').live('change', function() { $(this).parents('form:first').submit(); });
i've tried code, , problem seems on live method on javascript. works if change click function:
$(document).ready(function(){ $('.submittable').click(function() { $(this).parents('form:first').submit(); }); });
edit: forgot mention javascript included in app/assets/javascripts directory. because javascript must loaded on document.ready jquery ajax have effect want.
the arquive.js.erb file must exist in case have arquive action on controller, , javascript code in executed later. not case.
Comments
Post a Comment