Ruby-on-rails rails,如何使用 content_tag 在助手中构建表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3863844/
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:19:00  来源:igfitidea点击:

rails, How to build table in helper using content_tag?

ruby-on-railsruby-on-rails-3

提问by Adrian Serafin

I defined function in application helper:

我在应用程序助手中定义了函数:

def display_standard_table(columns, collection = {})    
  content_tag :table do      
    concat content_tag :thead do
      content_tag :tr do
        concat columns.collect { |column| content_tag(:td, column[:display_name]) }
      end
    end      

    concat content_tag :tbody do
      collection.collect { |elem| 
        concat content_tag(:tr, columns.collect { |column|
          content_tag(:td, elem.attributes[column[:name]])
      })
    }
    end
  end
end

and I call it in my view:

我认为它是这样的:

<%= display_standard_table(
          [
            { :name => 'id', :display_name => 'Id' },
            { :name => 'login', :display_name => 'Login' },
            { :name => 'first_name', :display_name => 'Name' },
            { :name => 'last_name', :display_name => 'LastName' },
            { :name => 'email', :display_name => 'Email'}
          ], @users) %>

The output in html is:

html中的输出是:

<table><thead></thead><tbody></tbody></table>

and it should be:

它应该是:

<table><thead><tr><td>Id</td><td>Login</td>...</tr></thead><tbody><tr>row here</tr></tbody></table>

and i can't figure out what's missing. (btw. I'm using rails3)

我无法弄清楚缺少什么。(顺便说一句。我正在使用 rails3)

[EDIT]

[编辑]

def display_standard_table(columns, collection = {})    
  content_tag(:table) do
    concat(content_tag(:thead) do
      concat(content_tag(:tr) do
        concat(columns.collect { |column| content_tag(:td, column[:display_name]) })
      end)
    end)

    concat(content_tag(:tbody) do
      concat(collection.collect { |elem|
        content_tag(:tr, columns.collect { |column|
          content_tag(:td, elem.attributes[column[:name]])
        })
      })
    end)
  end
end

The above version works ok but generated html is escaped :/ according to documentation strings produced by content_tag should be html_safe, but they're escaped :/

上面的版本工作正常,但生成的 html 被转义:/根据 content_tag 生成的文档字符串应该是 html_safe,但它们被转义了:/

回答by TemaMix

def display_standard_table(columns, collection = {})

 thead = content_tag :thead do
   content_tag :tr do
    columns.collect {|column|  concat content_tag(:th,column[:display_name])}.join().html_safe
   end
 end

 tbody = content_tag :tbody do
  collection.collect { |elem|
    content_tag :tr do
      columns.collect { |column|
          concat content_tag(:td, elem.attributes[column[:name]])
      }.to_s.html_safe
    end

  }.join().html_safe
 end

 content_tag :table, thead.concat(tbody)

end

回答by ramontiveros

Actually you are only missing a plus between content tags, you can find a similar question here

实际上,您只缺少内容标签之间的加号,您可以在此处找到类似的问题

Rails- nested content_tag

Rails 嵌套的 content_tag

The problem with this, is that maybe you will get a syntax error because of the way you are using the blocks, if you use a "{}" instead of "do end" statements you are going to be Ok.

这样做的问题是,由于您使用块的方式,您可能会遇到语法错误,如果您使用“{}”而不是“do end”语句,您将没问题。

Here (Change to the way the block is handled) you can find an example and a workaround in the case you use Rails 3 because apparently Rails 3 ignore the result of the collect statement when is inside of the content tag.

在这里(更改块的处理方式)您可以在使用 Rails 3 的情况下找到示例和解决方法,因为显然 Rails 3 在内容标记内部时忽略了 collect 语句的结果。

回答by shingara

See the railscast about that : http://railscasts.com/episodes/208-erb-blocks-in-rails-3all is explain how you manage your block helper in rails 3

请参阅有关该内容的 railscast:http://railscasts.com/episodes/208-erb-blocks-in-rails-3全部说明您如何在 rails 3 中管理块助手

回答by rfunduk

It's the missingconcatin your :trrelated content_tag.

这是缺少concat:tr相关content_tag

Update

更新

As for the escaping, take a look at the api docs for content_tag. You need to specify not to escape it. Of course, you'd be responsible for escaping the stuff inside the tags that you want escaped in that case.

至于转义,请查看.api 文档content_tag。您需要指定不转义它。当然,在这种情况下,您将负责转义要转义的标签内的内容。

回答by Scott

Adding '.join().html_safe' to the end of each collect section. That's what worked for me.

在每个收集部分的末尾添加“.join().html_safe”。这就是对我有用的东西。

  collection.collect { |elem| 
    concat content_tag(:tr, columns.collect { |column|
      content_tag(:td, elem.attributes[column[:name]])
  })
}.join().html_safe