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:
@data = get_locations
-
Add in your view inside of a form:
<%= select_location_tag :data %>
where “data” is the name of the form
Controller Methods¶ ↑
@data = 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.
Also, you can make this:
@data2 = get_locations({mode: "exclude", countries: ["Japan", "El Salvador", "Colombia", "Jamaica", "Spain"]})
will get all countries except these ones
@data3 = get_locations({mode: "include", countries: ["Japan", "El Salvador", "Colombia", "Jamaica", "Spain"]})
will get only these countries
Helper Methods¶ ↑
select_location_tag(form_name, params = {})
display the dropdowns for the locations. This is an example of a hash for params (only the key data
is required):
... 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" }, data: @data } ...
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.