自定义Rails * _for助手?

问题描述 投票:0回答:1

我发现自己在show视图中使用Rails的form_for来利用AR对象上的转换和其他方法来创建非交互式表单来显示该对象。尽管有一个定制的构建器,但必须有一个更好的方法。有没有这种功能的东西(gem或diy-tutorial)?我的搜索技能很差。

例如,如果我能写这样的东西,那就太好了:

<%= dl_for(@cog) do |dl| %>
  <%= dl.dt_dd(:name) %>
  <%= dl.dt_dd(:colors) { |colors| colors.to_sentence } %>
  <%= dl.dt_dd(:size, { class: @cog.size }) %>
<% end %>

并获得:

<dl>
  <dt>My Name Translation</dt>
  <dd>Cog 1</dd>

  <dt>My Colors Translation</dt>
  <dd>Red, Green and Blue</dd>

  <dt class="Small">My Size Translation</dt>
  <dd class="Small">Small</dd>
</dl>
ruby-on-rails actionview
1个回答
0
投票

您可以使用presenter模式的变体来创建自己的元素构建器:

class DefinitionListBuilder
  attr_accessor :object
  # context is the view context that we can call the rails helper
  # method on
  def initialize(object, context, **options)
    @object = object
    @context = context
    @options = options
    @i18n_key = object.model_name.i18n_key
  end

  def h
    @context
  end

  def dl(&block)
    @context.content_tag :dl do
      yield self
    end
  end

  def dt_dd(attribute, **options)
    h.capture do
      h.concat(h.content_tag :dt, translate_attribute(attribute), options)
      h.concat(h.content_tag :dd, object.send(attribute), )
    end
  end

  private
  def translate_attribute(attribute)
    key = "activerecord.attributes.#{@i18n_key}.#{attribute}"
    h.t(key)
  end
end

这相当于FormBuilder。实际上,这只是一个包装对象并提供范围限定于该对象的助手的类。然后,您创建一个帮助程序,该帮助程序创建元素生成器的实例:

module DefinitionListHelper
  def dl_for(record, **options, &block)
    builder = DefinitionListBuilder.new(record, self, options)
    builder.dl(&block)
  end
end

这等效于提供ActionView::Helpers::FormHelperform_for

为了简洁起见,对此进行了简化,并且#dd_dt不会阻塞。

示例:

# config/locales/se.yml
se:
  activerecord:
    attributes:
      cog:
        name: 'Namn'
        size: 'Storlek'
        color: 'Färg'
<%= dl_for(Cog.new(name: 'Cogsworth', size: 'biggish', color: 'red')) do |builder| %>
  <%= builder.dt_dd :name %>
  <%= builder.dt_dd :size %>
  <%= builder.dt_dd :color %>
<% end %>

HTML输出:

<dl>
  <dt>Namn</dt><dd>foo</dd>
  <dt>Storlek</dt><dd>m</dd>
  <dt>Färg</dt><dd>red</dd>
</dl>
© www.soinside.com 2019 - 2024. All rights reserved.