Question

all. I'm following the carmen-rails documentation, though I'm using rails4, and I can't get the state subregion working when selecting a country. In fact, when leaving the subregion code in place I can't even navigate to the site. I get this error:

localhost:3000

Processing by OrdersController#new as HTML
  Rendered orders/_subregion_select.html.erb (1.9ms)
  Rendered orders/_form.html.erb (773.3ms)
  Rendered orders/new.html.erb within layouts/application (775.8ms)
Completed 500 Internal Server Error in 784ms

ActionView::Template::Error (undefined method `downcase' for nil:NilClass):
    1: <div id="order_state_wrapper">
    2:   <% parent_region ||= params[:parent_region] %>
    3:   <% country = Carmen::Country.coded(parent_region) %>
    4: 
    5:   <% if country.nil? %>
    6:     <em>Please select a country above</em>
  app/views/orders/_subregion_select.html.erb:3:in `_app_views_orders__subregion_select_html_erb__937058573181156642_69893053026600'
  app/views/orders/_form.html.erb:100:in `block in _app_views_orders__form_html_erb__3775537416523760398_69893046471120'
  app/views/orders/_form.html.erb:1:in `_app_views_orders__form_html_erb__3775537416523760398_69893046471120'
  app/views/orders/new.html.erb:6:in `_app_views_orders_new_html_erb__3931135682021831649_69893046299220'

Doesn't look like the (country, "US" in this case) paramater is being passed from the parent region because it's "nil". Any insight to get this working (I'm assuming with rails4) ?

app/views/orders/_form.html.erb

<div class="control-group">
  <div class="field">
    <%= f.label :country, 'Country' %>
    <%= f.country_select :country, priority: %w(US CA), prompt: 'Please select a country' %>
  </div>
  <div class="field">
    <%= f.label :state %><br />
    <%= render partial: 'subregion_select', locals: {parent_region: f.object.country} %>
  </div>
</div>

app/views/orders/_subregion_select.html.erb

<div id="order_state_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, parent_region) %>
  <% else %>
    <%= text_field(:order, :state) %>
  <% end %>
</div>

app/assets/javascripts/orders.js.coffee

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

$ ->
  $('select#order_country').change (event) ->
    select_wrapper = $('#order_state_wrapper')

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

    country = $(this).val()

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

config/routes.rb

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

# rake routes

             Prefix      Verb   URI Pattern                          Controller#Action
orders_subregion_options GET    /orders/subregion_options(.:format)  orders#subregion_options

and when browsing to the subregion route directly and specifying a country:

http://localhost:3000/orders/subregion_options?parent_region=%22US%22

Started GET "/orders/subregion_options?parent_region=%22US%22" for 192.168.122.1 at 2013-07-08 13:20:21 -0400
Processing by OrdersController#subregion_options as HTML
  Parameters: {"parent_region"=>"\"US\""}
DEPRECATION WARNING: Relation#first with finder options is deprecated. Please build a scope and then call #first on it instead. (called from service at /usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:138)
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  Rendered orders/_subregion_select.html.erb (0.2ms)
Completed 200 OK in 4ms (Views: 1.3ms | ActiveRecord: 0.2ms)
Was it helpful?

Solution

The gem carmen-rails isn't registered on http://ready4rails4.net/, on the github page it written

carmen-rails is a Rails 3 plugin that supplies two new form helper methods: country_select and subregion_select.

plus on travis-ci.org https://travis-ci.org/jim/carmen-rails all builds are done with rails 3.2.

I would say the gem isn't Rails 4 ready maybe.

You should open an issue on the github page.


After having looked at your comment I have checked out the fork you're using and excepted the branch name I didn't saw any Rails 4 related commits.

Having a look at the changelog file (https://github.com/freerunningtechnologies/carmen-rails/blob/master/CHANGELOG.md) make me thinking that the rails 4 compatibility isn't done yet, but maybe I'm wrong.

OTHER TIPS

I got the same error on my Rails 3.2.15 application. I had to make one small change to the Carmen gem in order for it to work. Follow these instructions and you should be golden:

  1. In terminal at your project root, use bundle open carmen to open the Carmen gem library.
  2. Find the file lib/carmen/querying.rb.
  3. Replace line 15 with the following:

    code = code.try(:downcase) # Codes are all ASCII

I've submitted a pull request to the original author so this might get fixed soon in the master branch, but until then you can do the above or you can use my fork in your gemfile as follows:

gem 'carmen', github: 'joshuapinter/carmen'

Figured something that is causing of your error I also ran out.

<div id="order_state_code_wrapper"> 
  <% parent_region ||= params[:parent_region] %>
  <% unless parent_region.nil? %>
     <% country = Carmen::Country.coded(parent_region) %>
  <% end%>
  <% 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>

You need to add this to remove error

 <% unless parent_region.nil? %>
     <% country = Carmen::Country.coded(parent_region) %>
  <% end%>

unless is necessary on page load first time parent_region is nil that time

and remove /orders from url and routes since it is not calling controller action subregion_options and using subregion_options as id

modified url and routes

 url = "/subregion_options?parent_region=#{country}"

 get '/subregion_options' => 'orders#subregion_options'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top