Project

cs-rails

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Country and state selects for Rails, inspired by carmen-rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.18

Runtime

 Project Readme

cs-rails

Sometime ago I used carmen-rails and liked the project, but it's not maintained anymore. I also felt that it was not very magical and some simple spells could make it more sexy!

Instalation

gem 'cs-rails'

Usage

I only used with Simple Form and didn't write any test, so no country_select_tag for now, you must have a FormBuilder to use it.

First, you have to mount cs-rails engine: mount CsRails::Engine, at: '/cs'

Add the country select with/without priority countries.

= f_address.input :country, priority: ['BR', 'AR', 'CL', 'PY', 'UY'], input_html: { data: { cs_rails: true } }

You have to #= require cs-rails/countries. It will find any country input with data-cs-rails attribute and submit an ajax request to find the states for the selected country. If the states aren't find for the country, the state select is replaced by an input, so the user can write the state.

It will replace the contents for any select/input that have an id ending with "_state". If you want to control the select/input that must be replaced you use the state-input-id data attribute as following:

= f_address.input :country, priority: ['BR', 'AR', 'CL', 'PY', 'UY'], input_html: { data: { cs_rails: true, state_input_id: 'target_input_id' } }

Sometimes you need to set the country input data with $('#country_input_id').val(some_var), in these cases you need to trigger a 'change' event and you can pass a callback to execute after the states load:

$('select[id$=country]').val(some_var).trigger('change', fillState)

For now, it's all!

This project rocks and uses MIT-LICENSE.