Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Gus P asked in Rails

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
    });
  });

Reply

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
})
Reply

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) {}
        })

Reply

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

Reply

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

Reply

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.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.