Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails, How to build table in helper using content_tag?

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:

<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)

[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 :/

like image 995
Adrian Serafin Avatar asked Oct 05 '10 13:10

Adrian Serafin


3 Answers

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
like image 147
TemaMix Avatar answered Nov 06 '22 03:11

TemaMix


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

Rails- nested 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.

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.

like image 5
ramontiveros Avatar answered Nov 06 '22 04:11

ramontiveros


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

like image 1
shingara Avatar answered Nov 06 '22 05:11

shingara