Raman M Raman M - 3 months ago 9
jQuery Question

javascript ajax request in rails not able to pass values

I have a rails application in which I have following controller action.

def index
....
....
@currency = params["currency"].present? ? params["currency"] : "INR"
@check_in_date = params["arrival_date"].present? ? params["arrival_date"] : Date.today.to_s
@check_out_date = params["departure_date"].present? ? params["departure_date"] : (Date.today + 1).to_s
....
....
end


I have javascript where I am trying to make an ajax request like this.
filename.html.haml

else{
hotel_id = id.slice(6)
$.ajax({
url: "/single_hotel/"+hotel_id,
data: {check_in_date: #{@check_in_date}, check_out_date: #{@check_out_date}, longitude: #{@longitude}, latitude: #{@latitude}, rooms: #{@rooms}, adults: #{@adults}, children: #{@children}, currency: #{@currency} },
type: 'get'
});
}


when I check the sources tab in chrome console I see this.

$.ajax({
url: "/single_hotel/"+hotel_id,
data: {check_in_date: 2016-08-08, check_out_date: 2016-08-09, longitude: 34.854, latitude: 32.3213, rooms: 1, adults: 1, children: 0, currency: INR },
type: 'get'
});


When I try to make the ajax request I get "VM18204:52 Uncaught ReferenceError: INR is not defined".

Also if I remove currency and make the request I get following values for check in & check out dates.

[1] pry(#<Bookings::HotelsController>)> params
=> {"check_in_date"=>"2000",
"check_out_date"=>"1999",
"longitude"=>"34.854",
"latitude"=>"32.3213",
}


Can someone please help me here.

Answer

Instead of this

data: {check_in_date: #{@check_in_date}, check_out_date: #{@check_out_date}, longitude: #{@longitude}, latitude: #{@latitude}, rooms: #{@rooms}, adults: #{@adults}, children: #{@children}, currency: #{@currency} }

try this

data: {check_in_date: "#{@check_in_date}", check_out_date: "#{@check_out_date}", longitude: "#{@longitude}", latitude: "#{@latitude}", rooms: "#{@rooms}", adults: "#{@adults}", children: "#{@children}", currency: "#{@currency}" }
Comments