Ruby-on-rails 在 rails 3 中使用 helpers 输出 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3561250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:06:13  来源:igfitidea点击:

Using helpers in rails 3 to output html

ruby-on-railsrubyruby-on-rails-3html-helperactionviewhelper

提问by TheDelChop

I'm trying my best to build a helper that outputs a <'ul> consisting of all the members of a collection. For each member of the collection I want to print out a <'li> that has a title, and a div of links to CRUD the member. This is pretty similar to what Rails outputs for scaffolding for the index view.

我正在尽我最大的努力构建一个帮助器,它输出一个由集合的所有成员组成的 <'ul>。对于集合的每个成员,我想打印一个 <'li> ,它有一个标题,以及一个指向 CRUD 成员的链接的 div。这与 Rails 为索引视图的脚手架输出的内容非常相似。

Here is the helper I've got:

这是我的帮手:

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 

And that output exactly what I want. First of all, if anybody thinks there's a better way to do this, please feel free to correct me, I suspect that I'm doing this in a bass ackwards way, but at the moment its the only way I know how.

输出正是我想要的。首先,如果有人认为有更好的方法可以做到这一点,请随时纠正我,我怀疑我正在以一种向后低音的方式这样做,但目前这是我知道的唯一方法。

I then attempted to wrap the links in a div as follows:

然后我尝试将链接包装在一个 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 

However, I now no longer get any of the markup inside the div.links-bar output to the view. I'm sure this must have something to do with block and bindings, but I can for the life of me figure out what or how to go about fixing it. Can anybody offer any help?

但是,我现在不再获得 div.links-bar 输出中的任何标记到视图。我确定这一定与块和绑定有关,但我可以终生弄清楚修复它的方法或方法。有人可以提供任何帮助吗?

回答by Dave Pirotte

I agree with the comment above recommending the use of a partial... but if you DID need to do this in a helper, this is a cleaner way to implement:

我同意上面建议使用部分的评论......但如果你确实需要在助手中这样做,这是一种更清晰的实现方式:

def display_all(collection)
  content_tag(:ul, class: "list") do
    collection.collect do |member|
      concat(content_tag(:li, id: member.name.gsub(' ', '-').downcase.strip) do
        member.name
      end)
    end
  end
end

I'd pass in a collection explicitly rather than passing in a symbol to create a collection so you aren't always required to display ALL the records in a particular table at once. You could add pagination, etc.

我会显式传入一个集合,而不是传入一个符号来创建一个集合,因此您并不总是需要一次显示特定表中的所有记录。您可以添加分页等。

回答by Linh Chau

@Joe, You can still use your method display_all(collection_sym)Just use: return html.html_safeinstead of: return html

@Joe,您仍然可以使用您的方法display_all(collection_sym)只需使用: return html.html_safe而不是: return html

I still find that in many situations, it is better to generate HTML from helpers, instead of using partials. So the html_safefunction in Rails 3will make sure that you generate HTML, instead of converting it to String.

我仍然发现在许多情况下,最好从助手生成 HTML,而不是使用部分。因此html_safeRails 3 中的函数将确保您生成 HTML,而不是将其转换为String.

回答by James Hibbard

As @TheDelChop says, you need a concatfor the inner content_tag, otherwise the output is just <ul></ul>

正如@TheDelChop 所说,你需要一个concat用于 inner content_tag,否则输出只是<ul></ul>

Here's what that looks like:

这是它的样子:

def display_all(collection)
  content_tag(:ul, :class => "list") do
    collection.collect do |member|
      concat(
        content_tag(:li, :id => member.name.gsub(' ', '-').downcase.strip) do
          member.name
        end
      )
    end
  end
end

More explanation here: Nesting content_tag in Rails 3

更多解释在这里:Nesting content_tag in Rails 3