加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 为什么twitter-bootstrap-rails生成的视图使用

发布时间:2020-12-17 02:36:19 所属栏目:百科 来源:网络整理
导读:安装了twitter-bootstrap-rails,使用生成器: rails g bootstrap:themed Model 创建包含以下行的视图: th%= model_class.human_attribute_name(:comment_date) %/th th%= model_class.human_attribute_name(:comment_time) %/th 默认rails生成器直接插入人
安装了twitter-bootstrap-rails,使用生成器:

rails g bootstrap:themed Model

创建包含以下行的视图:

<th><%= model_class.human_attribute_name(:comment_date) %></th>
  <th><%= model_class.human_attribute_name(:comment_time) %></th>

默认rails生成器直接插入人类可读列名称:

<th>Comment date</th>
 <th>Comment time</th>

这使得视图更清晰,并且似乎在运行时更有效.

twitter-bootstrap-rails生成的视图有什么好处吗?

谢谢!

(PS,开发者网站是离线的,github和google都不允许发送私信,github上的“问题”和google帖子上的公开评论都不适合这个问题,所以希望有人在这里熟悉model_class.human_attribute_name()的用例

解决方法

看看它的来源,事情会很清楚:

# File activemodel/lib/active_model/translation.rb,line 45
def human_attribute_name(attribute,options = {})
  defaults  = []
  parts     = attribute.to_s.split(".",2)
  attribute = parts.pop
  namespace = parts.pop

  if namespace
    lookup_ancestors.each do |klass|
      defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
    end
    defaults << :"#{self.i18n_scope}.attributes.#{namespace}.#{attribute}"
  else
    lookup_ancestors.each do |klass|
      defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
    end
  end

  defaults << :"attributes.#{attribute}"
  defaults << options.delete(:default) if options[:default]
  defaults << attribute.humanize

  options.reverse_merge! :count => 1,:default => defaults
  I18n.translate(defaults.shift,options)
end

human_attribute_name可以很好地本地化您的属性名称.即使您正在构建仅使用英语的应用程序 – 谁知道,也许您的收件箱已经收到了客户关于其扩展业务计划的电子邮件,此扩展将包括人们从右向左书写的国家/地区.

这个怎么运作

假设您有具有title属性的Product model.以这种方式调用human_attribute_name:

Product.human_attribute_name 'title'

将使用以下参数调用I18n.t(请参阅上面代码段中的最后一行):

I18.t 'activerecord.attributes.product.title',{
  :count=>1,:default=>[:"attributes.title","Title"] 
}

因此,您可以提供特定于某些模型的翻译,并且特定于某些属性的名称.除非您提供任何这些翻译,否则您将获得通过人性化方法创建的属性的英文版本.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读