Giter Club home page Giter Club logo

carmen-demo-app's Introduction

carmen-rails Demo App

This app is a proof of concept to illustrate how to use carmen-rails inside a simple form in a Rails app.

carmen-rails depends upon carmen for its geographic data. If you want to customize the data generated by these helpers, the Carmen docs explain how.

Throughout this example, it is assumed that the code is operating on an Order model. Be sure to change all references to order to whatever is appropriate for your application.

Setup

Add carmen-rails to your Gemfile:

gem 'carmen-rails'

Use the new form helpers provided by carmen-rails to add country or subregion selects to your form:

<div class="field">
  <%= f.label :country_code %><br />
  <%= f.country_select :country_code, priority: %w(US CA), prompt: 'Please select a country' %>
</div>

That's all there is to it. If you want to support a subregion field and have the options inside it updated by value in the country select, place this input inside a partial:

<div class="field">
  <%= f.label :state_code %><br />
  <%= render partial: 'subregion_select', locals: {parent_region: f.object.country_code} %>
</div>

Here is the content of the subregion_select partial:

<div id="order_state_code_wrapper">
  <% parent_region ||= params[:parent_region] %>
  <% country = Carmen::Country.coded(parent_region) %>

  <% if country.nil? %>
    <em>Please select a country above</em>
  <% elsif country.subregions? %>
    <%= subregion_select(:order, :state_code, parent_region) %>
  <% else %>
    <%= text_field(:order, :state_code) %>
  <% end %>
</div>

Note that we're defaulting to a text field input when we don't have subregion information for a country, and that if we don't have a country at all, we show a simple message.

Now we will add a small script that replaces the subregion select when the country select's value changes. A wrapper div has been added to make this simpler.

$ ->
  $('select#order_country_code').change (event) ->
    select_wrapper = $('#order_state_code_wrapper')

    $('select', select_wrapper).attr('disabled', true)

    country_code = $(this).val()

    url = "/orders/subregion_options?parent_region=#{country_code}"
    select_wrapper.load(url)

Now we just need to add a route to config/routes.rb:

get '/orders/subregion_options' => 'orders#subregion_options'

And a basic controller action to the appropriate controller:

def subregion_options
  render partial: 'subregion_select'
end

And that's basically it. This is obviously a very simple example, but it can serve as the foundation of a variety of more involved interactions.

carmen-demo-app's People

Contributors

jim avatar pekeler avatar

Stargazers

SmartTech avatar

Watchers

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