我有 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 %>
有人用上面的宝石解决了同样的情况吗?感谢您的任何建议。米罗斯拉夫
形式:
解决方案
安装 gem Globalize Accessors ( https://github.com/globalize/globalize-accessors )
将以下代码添加到模型中:
globalize_accessors :locales => [:en, :cs], :attributes => [:description]
将以下代码添加到控制器:
def custom_params permitted = NonProfit.globalize_attribute_names + [:name] + [:ico] + [:website] + [:non_profit_category_id] params[:user][:non_profit].permit(*permitted) end
添加以下代码查看:
<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>