Rails unobtrusive javascript (UJS) ajax:before append data params

你。 提交于 2020-01-02 10:20:17

问题


I'm trying to append some parameters to a form before sending via ajax (via Rails UJS).

  $(document).on("ajax:before",".form-example", function(event) {
    event.data = $(":hidden").serialize();
  });

The hidden inputs aren't within the form, so I need to collect them, serialize the, and add them to the request (which is a PUT request). However, in the above example, no parameters are being passed to the server, am I doing something wrong? How can I pass parameter called "items" with the serialized values of hidden inputs?


回答1:


Looking into this, it is actually possible to do, but you do not use the event, you modify the form itself (using $(this)).

So something like this could work:

$(document).on("ajax:before",".form-example", function() {
  var form = $(this);
  form.append($('<input />',{name: 'hidden_values',value: $(":hidden").serialize()}));
});

And you should be able to access the data in your rails action using params[:hidden_values].



来源:https://stackoverflow.com/questions/26081297/rails-unobtrusive-javascript-ujs-ajaxbefore-append-data-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!