Forum: Ruby on Rails ajax, erb, uri, query string

15eb13d662803f57bc4aea59704988b4?d=identicon&s=25 Scott Ribe (Guest)
on 2012-07-10 23:43
(Received via mailing list)
Is there a cleaner way to build the url in the following snippet?

$(function() {
  $('#name').change(function() {
    $.ajax({
      url: "<%= url_for({action: 'lookup'}) %>" + "?term=" + this.value,
  ...
      }
    })
  })
})

Note that I should not use a hard-coded URL, because this app should be
deployed at a sub-URI using Passenger.

--
Scott Ribe
scott_ribe@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice
E7bf84a8c8f5143e8d90bc3caba15b14?d=identicon&s=25 Javier Quarite (Guest)
on 2012-07-11 00:16
(Received via mailing list)
On Tue, Jul 10, 2012 at 4:42 PM, Scott Ribe
<scott_ribe@elevated-dev.com>wrote:

> })
>
> Note that I should not use a hard-coded URL, because this app should be
> deployed at a sub-URI using Passenger.
>
>
As far as I understood, you want to do a request to the lookup action in
your controller.
I recently discovered this

   *select*(object, method, choices, options = {}, html_options = {})

inside your html_options hash you can put something like this

   {data: {remote: true, url: url_for(controller: "your controller (if
necessary)", action: "lookup")}}

so whenever you choose an option it will do an ajax request with the
selected value.
Then you just need a handler in that view, or just have a lookup.js.erb
file that handle the controller action

HTH

Javier Q.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.