简体   繁体   English

无法使Ruby on Rails 4 Ajax与Checkbox配合使用-Update Firing:但未更改复选框或数据库

[英]Can't get Ruby on Rails 4 Ajax working with Checkbox -Update Firing: but isn't changing checkbox or database

I'm attempting to use Ajax with Ruby on Rails 4 to watch when the checkbox changes and submit the form and save the checkbox change to the database. 我试图将Ajax与Ruby on Rails 4一起使用,以查看复选框何时更改,并提交表单并将复选框更改保存到数据库。 Even with the debugger I'm not seeing any errors. 即使使用调试器,我也看不到任何错误。 Any thoughts? 有什么想法吗?

I recently changed the path to edit because I was getting a 404. I did this to it matches a route. 我最近更改了编辑路径,因为我得到了404。我这样做是为了与路线匹配。

I just added the success function to the ajax call and it appears to be firing. 我刚刚将成功函数添加到了ajax调用中,它似乎正在触发。

This is what I have currently: 这是我目前拥有的:

This is product_controller 这是product_controller

def update
  if @product.update(safe_params)
    redirect_to [:edit, @product], flash: { notice: t('shoppe.products.update_notice') }
  else
    render action: 'edit'
  end
end

This is form in haml 这是HAML形式

= form_for product, :remote => true do |f|
    = f.check_box :active
    = f.label :active,  t('shoppe.products.active'), :remote => true

This is js: 这是js:

$('#product_active').bind('change', function() {
    console.log('changed');
    var action = $(this).parents('form').attr('action')+"/edit";
    var method = "GET";
    var checked = $(this).attr('checked');
    var data = $(this).attr('value');
    data ^= 1;

    $.ajax({
      method: method,
      url: action,
      data: checked,
      success: function() {
        alert("AJAX Fired");  
      }
    })

    debugger;

});

This is from rake routes 这是从耙路

products     GET      /products(.:format)            shoppe/products#index
             POST     /products(.:format)            shoppe/products#create
new_product  GET      /products/new(.:format)        shoppe/products#new
edit_product GET      /products/:id/edit(.:format)   shoppe/products#edit
product      GET      /products/:id(.:format)        shoppe/products#show
             PATCH    /products/:id(.:format)        shoppe/products#update
             PUT      /products/:id(.:format)        shoppe/products#update
             DELETE   /products/:id(.:format)

UPDATE: I ended up putting a hidden submit button with the form and changing my js to and adding in a class to the form to allow me to hit all ajax with one set of code: 更新:我最终在表单中放置了一个隐藏的提交按钮,并将我的js更改为并在表单中添加了一个类,以允许我使用一组代码来击打所有ajax:

$('.ajax').bind('change', function() {
    $(this).parents('form').submit();
});

You need to hit the update endpoint of your routes not the edit. 您需要点击路线的update端点,而不是编辑路线。 The edit route is usually to load the edit view. 编辑路径通常是加载编辑视图。

You'll also want to return a json encoded response in the update action and handle the success jquery callback and from there you would redirect in the client side or do whatever you want to do to update the UI. 您还需要在update操作中返回一个json编码的响应并处理success jquery回调,然后您将在客户端重定向或执行您想要做的任何操作来更新UI。 A redirect would be only useful from within your rails code if you were actually submitting a form and not doing ajax. 重定向仅在您实际提交表单而不执行ajax时才在rails代码中有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM