Skip to content

pablomarti/Pilgrim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pilgrim


Choose countries, states and cities via Ajax

Installation

  1. Add to your gemfile: gem 'pilgrim'

  2. Run bundle

  3. Execute: rails generate pilgrim:install

  4. Run migrations: rake db:migrate

  5. Run: rake db:seed

  6. Add to your routes.rb: mount Pilgrim::Engine => "/pilgrim", :as => "pilgrim"

  7. Add to your application.js: //= require pilgrim/application

  8. (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)

  9. Watch the test/dummy for an example (watch the welcome controller and its views)


Use

  1. Add in the action where you will need to load the countries for the view: @data = get_locations

  2. 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.

About

Choose countries, states and cities via Ajax

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages