如何在gem'carmen-rails','〜>中给出国家短名和国家短名。 1.0.0'

时间:2015-04-16 09:44:10

标签: ruby-on-rails rubygems

我在观点中已经说过:

<div class="input-control select country_input" data-role="input-control">
      <%= f.select :country, region_options_for_select(only_us_and_france),:prompt => 'Select Country' %>
</div>               
<div class="input-control select state_input" data-role="input-control">
          <%= render partial: 'subregion_select', locals: {parent_region: f.object.country} %>
 </div>

在我的分区域中,这是:

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

  <% if country.nil? %>
      <div style="display: none">State</div>
  <% elsif country.subregions? %>
      <%= subregion_select(:account_detail, :state_code, parent_region) %>
  <% else %>
      <%= text_field(:account_detail, :state_code) %>
  <% end %>
</div>

在我的帮手中:

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

所以它显示了国家和州的名称,但我希望在我的选择选项中显示,如US和FR等等。 请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个。

添加名为region_options_us_and_france的新助手方法:

def region_options_us_and_france
    Carmen::Country.all.select{|c| %w{US FR}.include?(c.code)}.map { |r| [r.code, r.code] }
end

def sub_region_options(region)
   region.subregions.map { |r| [r.code, r.code] }
end

并更新视图:

<div class="input-control select country_input" data-role="input-control">
      <%= f.select :country, region_options_us_and_france, :prompt => 'Select Country' %>
</div> 

更改&sub;部分&#39;

<%= subregion_select(:account_detail, :state_code, parent_region) %>

 <%= f.select :your_field_attr, sub_region_options(country), :prompt => 'Select Sub region' %>