一种形式的翻译字段(全球化+简单形式)

时间:2015-11-29 19:58:55

标签: ruby-on-rails simple-form globalize

我有simple_form(3.2.0)并与globalize(5.0.0)和Rails4结合使用,我想为用户提供一个选项,以相同的配置文件形式提供两种语言(捷克语和英语)的翻译。花了几个小时后,我陷入困境,不知道该怎么办。

<%= simple_form_for current_user, :url => user_path, html: { data: {type: 'script'}, id: "nonprofit-profile-form" }, remote: true, authenticity_token: true do |f| %>
    <%= f.simple_fields_for :non_profit, current_user.rolable do |np| %>

<div id="czech" class="tab-content" style="display: none;">
   <div class="row">
       <%= np.input :description, as: :text, label: false, :required => true, placeholder: t(:profiles_nonprofit_field_description), :input_html => { :rows => 16 } %>
   </div>
</div>

<div id="english" class="tab-content" style="display: none;">
   <div class="row">                         
       <%= np.input :description, as: :text, label: false, :required => true, placeholder: t(:profiles_nonprofit_field_description), :input_html => { :rows => 16 } %>
   </div>
</div>

    <% end %>
<% end %>

任何使用上述宝石解决同一案件的人?谢谢你的任何建议。米罗

形式:

enter image description here

  1. 安装gem Globalize Accessors(https://github.com/globalize/globalize-accessors

  2. 将以下代码添加到模型中:

    globalize_accessors :locales => [:en, :cs], :attributes => [:description]
    
  3. 将以下代码添加到控制器:

    def custom_params
        permitted = NonProfit.globalize_attribute_names + [:name] + [:ico] + [:website] + [:non_profit_category_id]
        params[:user][:non_profit].permit(*permitted)
    end
    
  4. 添加以下代码以查看:

    <div id="cestina" class="tab-content" style="display: none;">
        <div class="row">
            <%= np.input :description_cs, as: :text, label: false, :required => true, placeholder: t(:profiles_nonprofit_field_description), :input_html => { :rows => 16 } %>
            <div class="remainChars"><span class="usedChars">0</span>/1000</div>
         </div>
    </div>
    
    <div id="english" class="tab-content" style="display: none;">
         <div class="row">
             <%= np.input :description_en, as: :text, label: false, :required => true, placeholder: t(:profiles_nonprofit_field_description), :input_html => { :rows => 16 } %>
             <div class="remainChars"><span class="usedChars">0</span>/1000</div>
          </div>
    </div>  
    

1 个答案:

答案 0 :(得分:3)

好的,我找到了解决方案:

  1. 安装gem Globalize Accessors(https://github.com/globalize/globalize-accessors

  2. 将以下代码添加到模型中:

    globalize_accessors :locales => [:en, :cs], :attributes => [:description]
    
  3. 将以下代码添加到控制器:

    def custom_params
        permitted = NonProfit.globalize_attribute_names + [:name] + [:ico] + [:website] + [:non_profit_category_id]
        params[:user][:non_profit].permit(*permitted)
    end
    
  4. 添加以下代码以查看:

    <div id="cestina" class="tab-content" style="display: none;">
        <div class="row">
            <%= np.input :description_cs, as: :text, label: false, :required => true, placeholder: t(:profiles_nonprofit_field_description), :input_html => { :rows => 16 } %>
            <div class="remainChars"><span class="usedChars">0</span>/1000</div>
         </div>
    </div>
    
    <div id="english" class="tab-content" style="display: none;">
         <div class="row">
             <%= np.input :description_en, as: :text, label: false, :required => true, placeholder: t(:profiles_nonprofit_field_description), :input_html => { :rows => 16 } %>
             <div class="remainChars"><span class="usedChars">0</span>/1000</div>
          </div>
    </div>