1

全部。尽管我使用的是 rails4,但我正在关注carmen-rails文档,并且在选择国家/地区时无法使州次区域正常工作。事实上,当保留子区域代码时,我什至无法导航到该站点。我收到此错误:

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'

看起来(国家,在这种情况下为“US”)参数不是从父区域传递的,因为它是“nil”。有什么见解可以让这个工作(我假设使用 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>

应用程序/资产/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)

配置/路由.rb

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

# rake 路由

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

当直接浏览子区域路线并指定国家时:

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)
4

3 回答 3

1

gem carmen-rails 没有在http://ready4rails4.net/上注册,在它写的 github 页面上

carmen-rails 是一个 Rails 3 插件,它提供了两个新的表单辅助方法:country_select 和 subregion_select。

加上 travis-ci.org https://travis-ci.org/jim/carmen-rails所有构建都是使用 rails 3.2 完成的。

我会说这颗宝石可能还没有准备好 Rails 4。

您应该在 github 页面上打开一个问题。


在查看了您的评论后,我检查了您正在使用的分叉,但分支名称除外,我没有看到任何与 Rails 4 相关的提交。

查看变更日志文件(https://github.com/freerunningtechnologies/carmen-rails/blob/master/CHANGELOG.md)让我认为rails 4兼容性尚未完成,但也许我错了.

于 2013-07-08T18:19:22.327 回答
0

我在 Rails 3.2.15 应用程序上遇到了同样的错误。我必须对 Carmen gem 做一点小改动才能让它工作。按照这些说明,你应该是金子:

  1. 在项目根目录的终端中,使用bundle open carmen打开 Carmen gem 库。
  2. 找到文件lib/carmen/querying.rb
  3. 将第 15 行替换为以下内容:

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

我已经向原作者提交了一个拉取请求,所以这可能很快会在 master 分支中得到修复,但在那之前你可以执行上述操作,或者你可以在 gemfile 中使用我的 fork,如下所示:

gem 'carmen', github: 'joshuapinter/carmen'
于 2013-11-09T08:24:08.937 回答
0

想出了导致您的错误的原因,我也用完了。

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

您需要添加它以消除错误

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

除非在第一次加载页面时需要 parent_region 那个时候是 nil

并从url路由中删除/orders ,因为它没有调用控制器操作 subregion_options 并使用 subregion_options 作为 id

修改后的 url 和路由

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

 get '/subregion_options' => 'orders#subregion_options'
于 2014-01-03T13:05:16.697 回答