我正在努力构建一个帮助器,输出一个由集合的所有成员组成的<'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
Run Code Online (Sandbox Code Playgroud)
而那输出正是我想要的.首先,如果有人认为有更好的方法可以做到这一点,请随意纠正我,我怀疑我是以低音方式做到这一点,但此刻它是我知道如何的唯一方式.
然后我尝试将链接包装在div中,如下所示:
def display_all(collection_sym)
collection = collection_sym.to_s.capitalize.singularize.constantize.all …
Run Code Online (Sandbox Code Playgroud) ruby html-helper ruby-on-rails actionviewhelper ruby-on-rails-3
如何使用 content_tag 帮助程序获得以下 html 输出?
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
这是我目前所拥有的
content_tag(:ul) do
a=*(1..5)
a.each do |step_number|
content_tag(:li, class: "step") do
puts step_number
end
end
end
Run Code Online (Sandbox Code Playgroud)
感谢 Peter's Loop & output content_tags 在下面的帮助链接中的content_tag 中,我遗漏concat
了这些li
项目
content_tag :ul do
items.collect do |step_number|
concat(content_tag(:li, step_number, class: "step"))
end
end
Run Code Online (Sandbox Code Playgroud)