How can I use jQuery UJS to submit a form via AJAX after the change event of a select box fires in Rails? -
how can use jquery ujs submit form after change event of select box fires in rails?
my view looks this:
<% perm in @permissions %> <%= form_for [@brand,perm], { :remote => true } |f| %> <tr id="permission_<%= perm.id %>"> <td><%= perm.configurable.name %></td> <td><%= f.select :action, ['none', 'read', 'update', 'manage'] %></td> </tr> <% end %> <% end %>
pretty straightforward. controller so:
class permissionscontroller < applicationcontroller before_filter :authenticate_user! respond_to :html, :js load_and_authorize_resource :brand load_and_authorize_resource :permission, :through => :brand def index end def update @permission = permission.find(params[:id]) @permission.update_attributes(params[:permission]) end end
and in application.js:
// place application-specific javascript functions , classes here // file automatically included javascript_include_tag :defaults $(function() { $("#permission_action").change(function() { this.form.submit(); // needs ajax call, how? }); })
please note form submission fires fine. doing regular post rather ajax submission. when place submit button on form , use that, ajax submission works fine. need change:
this.form.submit();
to ajax call, don't know how. can help?
i see using jquery that's good. following code taken here: http://railscasts.com/episodes/136-jquery
i advise familiar these screen casts ton (understatement).
// public/javascripts/application.js jquery.ajaxsetup({ 'beforesend': function(xhr) {xhr.setrequestheader("accept", "text/javascript")} }) jquery.fn.submitwithajax = function() { this.submit(function() { $.post(this.action, $(this).serialize(), null, "script"); return false; }) return this; }; $(document).ready(function() { $("#new_review").submitwithajax(); })
Comments
Post a Comment