Pilgrim
Choose countries, states and cities via Ajax
Installation
-
Add to your gemfile:
gem 'pilgrim'
-
Run
bundle
-
Execute:
rails generate pilgrim:install
-
Run migrations:
rake db:migrate
-
Run:
rake db:seed
-
Add to your routes.rb:
mount Pilgrim::Engine => "/pilgrim", :as => "pilgrim"
-
Add to your application.js:
//= require pilgrim/application
-
(Optional) Generate the views so you can configure them executing:
rails generate pilgrim:generate_views
(this will save the views in your_project/app/views/pilgrim) -
Watch the test/dummy for an example (watch the welcome controller and its views)
Use
-
Add in the action where you will need to load the countries for the view:
get_locations
-
Add in your view inside of a form:
<%= select_location_tag :data %>
where “data” is the name of the form
Controller Methods
get_locations(country_id = 0, state_id = 0, city_id = 0)
gets the countries, the states of the first selected country, and the cities of the first state inside of 3 collections: @countries, @states, @cities. The parameters are for pre-select the options in the dropdowns.
Helper Methods
select_location_tag(form_name, params = {})
display the dropdowns for the locations. This is an example of a hash for params:
...
params = {
block: "main",
country: {
name: "country_field_name",
html_class: "some_class"
},
state: {
name: "state_field_name",
html_class: "some_class"
},
city: {
name: "city_field_name",
html_class: "some_class"
}
}
...
Where block
defines an id, so you can use the helper as many times you want changing the value in this key; the configurations for country, state and city are separated and in each one you can define the field name and the class you want to use for each element.