I currently have a city that someone will enter into the system and I want to go from my javascript action to a rails action so i can get the city.id from the database. For example
my javascript
function get_city(city){
//city comes in like "Washington DC"
}
my rails action
def return_city_id
@city = City.find_by_name(params["name"])
return @city.id
end
Try ajax. You can setup path in
routes.rblikefind_city_id_by_name?name=Washington DC. Then you can use jquery to send request.In controller, you’ll need to write single id as request response:
More information:
http://api.jquery.com/jQuery.ajax/
Although what you ask is definitely doable, querying back-end each time you need to fetch city id by its name sounds like a bad idea to me. If the name itself come from back-end (for example, you have list of cities for user to choose from), it’s better to provide ids together with names (not visible to user, but somewhere in html).