开发者

Best way to output a tag who's name is dynamically created?

In my Rails application I have a partial which is called开发者_Python百科 recursively.

In the partial I want to output a <h1>, <h2>, <h3> ... depending on the level. (Cap at level 6, of course)

Something like this:

<h1>
  <p><%= ... %></p>
  <% books.each do |book| %>
      ...
  <% end %>
</h1>

------->
<% open_h(1) %>
  <p><%= ... %></p>
  <% books.each do |book| %>
      ...
  <% end %>
<% close_h(1) %>

For now I hacked together the two functions as helpers, but is that really the most elegant way to do this?


You can do something like

# _book.html.erb

<% content_tag "h#{level}" do %>
  <p><%= ... %></p>
  <% if level < 6 %>
  <%= render :collection => books, :locals => { :level => level + 1 }
  <% end %>
<% end %>

# action.html.erb

<%= render :partial => :book, :collection => @books, :locals => { :level => 0 } %>


I'm not sure I got what you want exactly, but have a look at content_tag and tag helpers and share some of your final code so that we can help more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜