[英]Rails helper doesn't render all content
I have some code which I use on many of my pages, and it's starting to get kind of laborious to go back and update everything when changes are made.我有一些代码在我的许多页面上使用,并且在进行更改时返回并更新所有内容开始变得有点费力。
Trying to DRY up my code I thought it would be a good idea to convert the commonly used code into a helper method.尝试整理我的代码时,我认为将常用代码转换为辅助方法是个好主意。 After working on this for a while, Im still having trouble getting my helper to completely render all of the html generated, and I cant figure out what Im doing wrong.对此进行了一段时间的研究后,我仍然无法让我的助手完全呈现生成的所有 html,而且我无法弄清楚我做错了什么。
here is my helper:这是我的帮手:
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
What am I doing wrong here?我在这里做错了什么?
An issue that I can see is that your helpers does not "concatenate" all of the generated content:我可以看到的一个问题是您的助手不会“连接”所有生成的内容:
def link_table(collection, *args)
content_tag :ul, class: "table help" do
content_tag :li, class: "header" do
args.collect do |arg|
# ...
end.join(' ').html_safe
end
# RIGHT HERE, the above is not "concatenated" to the content below
collection.collect do |object|
content_tag :li do
args.collect do |arg|
# ...
end.join(' ').html_safe
end
end.join(' ').html_safe
end
end
Usually, for helpers of this type (generating HTML), I do the following pattern:通常,对于这种类型的助手(生成 HTML),我执行以下模式:
def link_table(collection, *args)
content = ''.html_safe
content += content_tag(:li, class: 'header') do
# ...
end
content += collection.collect do |object|
content_tag :li do
# ...
end
end.join(' ')
content_tag(:ul, class: 'table help') do
content
end
end
Also, I would suggest you to DRY your li
content tag with class header
as they are the same for your 3 helpers.另外,我建议您使用类header
干燥您的li
内容标签,因为它们对于您的 3 个助手是相同的。 Something like:就像是:
def li_header(collection)
content_tag :li, class: "header" do
collection.collect do |item|
content_tag :span, item.to_s.titleize
end.join(' ').html_safe
end
end
and use it like this:并像这样使用它:
def link_table(collection, *args)
content = ''.html_safe
content += li_header(collection)
# ...
content_tag(:ul, class: 'table help') do
content
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.