1

I'm trying to get the variable "response.data.uri" from my jQuery to a rails controller, but it isn't working. I can get to the rails controller but the variable isnt there.

jQuery:

function responseCallbackHandler(response) {
   switch (response.status) {
     case 201:
         $.ajax({ url: '#{addbank_bankaccts_path}',
         type: 'POST',
         beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
         dataType: "json",
         data: 'account_uri=' + response.data.uri
         success: function(response) {
           // successfully executed the controller method
           }
         });
         break;
     }
 }

routes.rb:

post "/bankaccts/new" => 'bankaccts#addbank' 

controller:

class BankacctsController < ApplicationController

  def addbank
      @uri = (params['customer_uri'])
      @id = (params['id'])
      @auri = (params['account_uri'])
      # all equal nil ^
      redirect_to root_url
  end
7
  • I don't see you using params['account_uri'], have you tried that?
    – vee
    Commented Aug 7, 2013 at 3:55
  • no params['account_uri'] is nil Commented Aug 7, 2013 at 4:01
  • Check your params hash in your controller to make sure data is coming through and also check if response.data.uri has your expected value in the browser console.
    – vee
    Commented Aug 7, 2013 at 4:06
  • response.data.uri indeed has the value I want in the js let me check the params Commented Aug 7, 2013 at 4:13
  • yeah nothing in params when i get to the controller Commented Aug 7, 2013 at 4:20

2 Answers 2

2

Ah, I know: In ruby, single quotes do not perform variable interpolation. So if you posted the jquery as it appears in the browser, the url for you ajax request would be:

'#{addbank_bankaccts_path}'

...which is gibberish--a browser does not understand ruby or rails. Url's in a browser must look like:

http://mysite.com/js/myjs.js
js/myjs.js

If your jquery is in a file whose name ends with .erb, then before sending the file to the browser rails will execute any ruby code in the file, but single quotes in ruby do not perform variable interpolation. Here is an example of that:

name = 'Dave'
str = '#{name} says hello.'
puts str

--output:--
#{name} says hello

But with double quotes:

name = 'Dave'
str = "#{name} says hello."
puts str

--output:--
Dave says hello.

I don't know if you are coming from a python background or not, where all quotes are equivalent; but in a lot of languages, including ruby, single and double quotes are not equivalent.

Your form_authenticity_token has the same problem:

beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
0

Try -

data: {account_uri: response.data.uri, 'customer_uri': SOME_VALUE, 'id': SOME_VALUE}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.