コレクションのすべてのメンバーで構成される<'ul>を出力するヘルパーを構築するために最善を尽くしています。コレクションのメンバーごとに、タイトルとメンバーをCRUDするためのリンクのdivを持つ<'li>を印刷したいと思います。これは、Railsがインデックスビューのスキャフォールディングに対して出力するものと非常によく似ています。
これが私が持っているヘルパーです:
def display_all(collection_sym)
collection = collection_sym.to_s.capitalize.singularize.constantize.all
name = collection_sym.to_s.downcase
html = ''
html << "<ul class=\"#{name}-list\">"
for member in collection do
html << content_tag(:li, :id => member.title.gsub(' ', '-').downcase.strip) do
concat content_tag(:h1, member.title, :class => "#{name}-title")
concat link_to 'Edit', "/#{name}/#{member.id}/edit"
concat "\|"
concat link_to 'View', "/#{name}/#{member.id}"
concat "\|"
concat button_to 'Delete', "/#{name}/#{member.id}", :confirm => 'Are you sure? This cannot be undone.', :method => :delete
end
end
html << '</ul>'
return html
end
そして、それはまさに私が望むものを出力します。まず第一に、誰かがこれを行うためのより良い方法があると思うなら、遠慮なく私を訂正してください、私はこれを低音の厄介な方法で行っていると思いますが、現時点ではそれが私が知っている唯一の方法です。
次に、次のようにリンクをdivでラップしようとしました。
def display_all(collection_sym)
collection = collection_sym.to_s.capitalize.singularize.constantize.all
name = collection_sym.to_s.downcase
html = ''
html << "<ul class=\"#{name}-list\">"
for member in collection do
html << content_tag(:li, :id => member.title.gsub(' ', '-').downcase.strip) do
concat content_tag(:h1, member.title, :class => "#{name}-title")
concat content_tag(:div, :class => "links-bar") do
concat link_to 'Edit', "/#{name}/#{member.id}/edit"
concat "\|"
concat link_to 'View', "/#{name}/#{member.id}"
concat "\|"
concat button_to 'Delete', "/#{name}/#{member.id}", :confirm => 'Are you sure? This cannot be undone.', :method => :delete
end
end
end
html << '</ul>'
return html
end
ただし、ビューへのdiv.links-bar出力内のマークアップは取得できなくなりました。これはブロックとバインディングと関係があるはずですが、私は一生の間、それを修正するために何をどのように行うかを理解することができます。誰か助けてもらえますか?