Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

How do I send params from Rails Ajax to the controller???

Rails • Asked by Gus P

I am trying to send all the params from the form into my controller after a promise

my params include :name , :email , :address_line1, :address_city, :address_state, :address_zipcode, :address_country, :custom, :subscription, :cc, :plan

Is there aw ay to send them all at once to the controller? I know my data prop is incorrect any help would be GREATLY appreciated.

  form.addEventListener('submit', function(event) {
  event.preventDefault();
    if ($('#amount').not(':empty')){
      $("#poker_button").attr("disabled", true);
      $("#poker_button").html('We are processing your donation, please wait');
      $("#poker_button").css('background-color', '#28a745');
    }

  stripe
    .createpaymentmethod({
      type: 'card',
      card: cardnumberelement,
      billing_details: {
        name: $('#name').val(),
      },
    })
    .then(function(result) {
      if(result.error) {
        console.log(result.error)
      } else {
        console.log(result.paymentmethod.id)
        console.log("winning")
        $("#cc").val(result.paymentmethod.id)

        rails.ajax({
          url: "/donations",
          type: "post",
          data: { #{ params }  },
          success: function(data) {
            console.log("foo")
          },
          error: function(data) {}
        })
      }
      // handle result.error or result.paymentmethod
    });
  });

Hey Gus!

You would just pass in a Javascript object into it, similar to a Ruby hash.

Rails.ajax({
  url: '/donations',
    type: 'post',
    data: {
      amount: 900,
    }
})

Or

params = { amount: 900 }
Rails.ajax({
  url: '/donations',
    type: 'post',
    data: params
})

What I ended up doing was this

        var formData = new FormData(form)

        Rails.ajax({
          url: "/donations",
          type: "post",
          data: formData,
          success: function(data) {
          },
          error: function(data) {}
        })

That works too! And will allow you to send files as well.


Ok here's a follow up to this , so the form gets sent to donations create everything works fine but then I try to redirect the user to thank_you_path and it doesnt but it in the logs it says it does

https://gist.github.com/staycreativedesign/366964b45f3b295b5325d8c7a6c55d6d


Yeah, AJAX requests don't work like a normal form submit. Redirects won't be followed by the browser like a normal page view, since you didn't make a normal request. You can redirect client side in the success callback if you want, or have the server do it in a create.js.erb response.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,242+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.