ajax, erb, uri, query string

Is there a cleaner way to build the url in the following snippet?

$(function() {
  $('#name').change(function() {
      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.

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


Javier Q.