0

次のように、単一のオブジェクトにパーシャルを使用しています。

#/app/views/people/_person.html.haml
%div[person]
  = image_tag person.image
  %p.name= person.full_name
  %p.location= person.location

...そのため、render次のようにオブジェクトのグループにメソッドを使用できます。

#/app/views/people/index.html.haml
%ul.people
  - @people.each do |person|
    %li= render person

しかし、オブジェクトを 2 回以上レンダリングすると、同じ ID を持つ 2 つの要素が取得されます。これは、検証のためだけであれば、あまり良くありません。上記の単一のオブジェクトを部分的に保持し、後でrenderを呼び出すときに、IDまたは何かをプレフィックスとしてIDを一意に保つように指示する方法はありますか?

例を見てみましょう。たとえば、私は一種の Facebook クローンを持っています。サイドバーにはあなたが興味を持ちそうな人のリストがあり、メイン エリアにはすべての友達のリストがあり (私は現在人のインデックス ビューにいます)、ヘッダーには最近試した人のリストがあります。私に連絡する。これら 3 つのセットは同じ人物を共有している可能性があるため、同じ ID を持つ複数の要素を取得します。person_654Haml がブラケット表記で行うように、すべての id を与える代わりに、サイドバーの人物に id interesting_person_654、ヘッダーの人物に id lately_calling_person_654、メイン エリアの人物 (現在のビュー) id person_654.

どうすればいいですか?

名前空間にいくつかのオプションを使用することを考えましたが、render メソッドには 4 つのオプションしかなく、名前空間はそれらの 1 つではありません。ただし、render メソッドにモンキーパッチを適用しても問題ありません。むしろ、id プレフィックス ロジックでオブジェクト パーシャルを不明瞭にしたくありません (他のすべてのオブジェクト パーシャルにコピー アンド ペーストする必要さえありますが、まったく良くありません!)。

4

2 に答える 2

0

さて、私は解決策を見つけました:

括弧内にレコードを渡す場合、haml は recordshaml_object_refメソッドを使用して、クラスと id プレフィックスに使用する値を決定します。したがって、レンダリング前にこのメソッドを適宜調整できるように、レンダリング ヘルパーを書き直しました。

# config/initializers/extensions/action_view_extension.rb
#
# (For some reason that is beyond be I could override this method in
# ActionView::Helpers::RenderingHelper, so for not I patched it at class level)
#
class ActionView::Base

  alias_method :original_render, :render

  def render objects = [], options = {}, *args, &block
    class_prefix = options[:class_prefix] || options[:namespace] || options[:as]
    records = Array( objects ).select { |object| object.is_a? ActiveRecord::Base }
    records.each do |record|
      class_name = record.class.to_s.underscore
      record.define_singleton_method :haml_object_ref do
        [class_prefix, class_name].compact.join( '_' )
      end
    end

    original_render objects, options, *args, &block

  end
end

renderこれで、次のようなビューを呼び出すことができます:

#header
  %ul.lately_calling_people
    %li = render @lately_calling_people[0], as: 'lately_calling'
    %li = render @lately_calling_people[1], as: 'lately_calling'
    %li = render @lately_calling_people[2], as: 'lately_calling'

#sidebar
  %ul.interesting_people
    %li= render @interesting_people[0], as: 'interesting'
    %li= render @interesting_people[1], as: 'interesting'
    %li= render @interesting_people[2], as: 'interesting'

#main
  %div[@person]
    @person.name
    ...

これにより、次の html が生成されます。

...

<ul class="lately_calling_people"
  <li><div class="lately_calling_person" id="lately_calling_person_1">...</div></li>
  ...
</ul>

...

<ul class="interesting_people"
  <li><div class="interesting_person" id="interesting_person_1">...</div></li>
  ...
</ul>

...

<div class="person" id="person_1">
  ...
</div>

そして、部分をシンプルに保つことができます:

# app/views/people/_person.html.haml
%div[person]
  = image_tag person.image
  %p.name= person.full_name
  %p.location= person.location
于 2013-11-07T07:10:39.283 に答える
0

ドキュメントからHaml のオブジェクト参照構文:

さらに、2 番目の引数 (存在する場合) は、id 属性と class 属性の両方のプレフィックスとして使用されます。

したがって、次のように変更できます。

%div[person]

に (例):

%div[person, :interesting]

次のようなものが生成されます。

<div class='interesting_person' id='interesting_person_7'>...

じゃあ後で:

%div[person, :lately_calling]

作成します:

<div class='lately_calling_person' id='lately_calling_person_7'>
于 2013-11-04T01:44:17.990 に答える