Giter Club home page Giter Club logo

carmen-rails's Introduction

carmen-rails

carmen-rails is a Rails plugin that supplies two new form helper methods: country_select and subregion_select. It uses carmen as its source of geographic data.

Requirements

carmen-rails requires Ruby 1.9.2 or greater.

Installation

Just add carmen-rails to your Gemfile:

gem 'carmen-rails'

Usage

<%= form_for(@order) do |f| %>
  <div class="field">
    <%= f.label :country_code %><br />
    <%= f.country_select :country_code, {priority: %w(US CA), prompt: 'Please select a country'} %>
  </div>
<% end %>

SimpleForm

Pass the object to the country_select helper. This ensures the persisted country is selected when the form is rendered.

<%= simple_form_for @user do |f| %>
  <%= f.input :country_code do %>
    <%= f.country_select :country_code, {object: f.object, prompt: 'Country'} %>
  <% end %>
<% end %>

Passing the object is necessary when using nested form fields with Formtastic.

How do I only display a subset of countries/regions?

Carmen had a concept of excluded countries in the old days, where you could specify certain countries or regions to not include in a select.

The new (and much more flexible) way to handle this is to write a helper method that returns the subset of regions you want to provide:

def only_us_and_canada
  Carmen::Country.all.select{|c| %w{US CA}.include?(c.code)}
end

And then in your form something like this:

<%= f.select :country, region_options_for_select(only_us_and_canada) %>

Configuration

Using this library will automatically set Carmen to use Rails' built-in I18n functionality. This means that changing some configuration should be done through Rails and not Carmen. For example, adding paths for additional locale files should be done inside config/application.rb:

config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]

carmen-rails's People

Contributors

abrom avatar bshelton229 avatar cdainmiller avatar ecbypi avatar jgrevich avatar jim avatar kevinwmerritt avatar martinbcg avatar mcrowe avatar panckreous avatar petergoldstein avatar rdsoze avatar tinynumbers avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.