【发布时间】:2018-02-07 17:36:10
【问题描述】:
我有一些代码在我的许多页面上使用,并且在进行更改时返回并更新所有内容开始变得有点费力。
尝试干掉我的代码我认为将常用代码转换为辅助方法是个好主意。经过一段时间的研究,我仍然无法让我的助手完全渲染所有生成的 html,而且我无法弄清楚我做错了什么。
这是我的助手:
module TablesHelper
def table(collection, *args)
content_tag :ul, class: "table help" do
#first off we need a header row
content_tag :li, class: "header" do
args.collect do |option|
content_tag :span, option.to_s.titleize
end.join(' ').html_safe
end
collection.collect do |object|
content_tag :li do
args.collect do |param|
content_tag :span, object.send(param)
end.join(' ').html_safe
end
end.join(' ').html_safe
end
end
def link_table(collection, *args)
content_tag :ul, class: "table help" do
#first off we need a header row
content_tag :li, class: "header" do
args.collect do |arg|
content_tag :span, arg.to_s.titleize
end.join(' ').html_safe
end
collection.collect do |object|
content_tag :li do
args.collect do |arg|
#the first item on this row needs to be a link
if arg.equal? args.first
link_to object.send(arg), object
else
content_tag :span, object.send(arg)
end
end.join(' ').html_safe
end
end.join(' ').html_safe
end
end
def button_table(collection, *args)
content_tag :ul, class: "table help" do
#first off we need a header row
concat content_tag :li, class: "header" do
args.collect do |option|
concat content_tag :span, option.to_s.titleize
end.join(' ').html_safe
end
collection.collect do |object|
concat content_tag :li do
args.collect do |param|
concat content_tag :span, object.send(param)
end.join(' ').html_safe
#lets throw on the small buttons
concat content_tag :div, class: "options" do
concat link_to content_tag(:i, nil, :class => "fa fa-eye"), object, class: "small primary button"
concat link_to content_tag(:i, nil, :class => "fa fa-pencil"), [:edit, object], class: "small primary button"
end
end
end.join(' ').html_safe
end
end
end
我在这里做错了什么?
【问题讨论】:
-
你是用
<%= your_helper %>(如果使用ERB)还是= your_helper(如果使用HAML)使用这些助手? -
您的
li和html 类header似乎没有被呈现。我正在准备一个答案。 -
你可能会更好地将其构建为可重用的部分,YMMV。
标签: ruby-on-rails helper