我正在尽力构建一个帮助器,它输出一个由集合的所有成员组成的 <'ul> 。对于集合的每个成员,我想打印出一个 <'li> ,它有一个标题,以及一个指向该成员的 CRUD 链接的 div。这与 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 输出中的任何标记发送到视图。我确信这一定与块和绑定有关,但我可以在我的一生中弄清楚要修复它的内容或方法。任何人都可以提供任何帮助吗?