Search code examples
ruby-on-railsrefinerycms

How to render specific number of times?


How to render specific number of times ? I'm making that my topmenu and sidemenu have the same pages.I want for the topmenu to only show first 4 pages and for the sidemenu to show all available pages.

<%= render :partial => '/shared/menu_branch', :collection => roots, :locals => {

           :hide_children => hide_children,
           :sibling_count => (roots.length - 1),
           :apply_css => true 
} -%>

and the menu_branch

<%
  if !!local_assigns[:apply_css] and (classes = menu_branch_css(local_assigns)).any?
    css = "class='#{classes.join(' ')}'".html_safe
  end
  dom_id = "id='item_#{menu_branch_counter}'".html_safe if menu_branch.parent_id.nil?
-%>

<li<%= ['', css, dom_id].compact.join(' ').gsub(/\ *$/, '').html_safe %>>
  <%= link_to menu_branch.title, menu_branch.url -%>
</li>

this is the line i want to render only 4 times.

<li<%= ['', css, dom_id].compact.join(' ').gsub(/\ *$/, '').html_safe %>>
  <%= link_to menu_branch.title, menu_branch.url -%>
</li>

Solution

  • Alright I figured out it by my self. This is the code I changed.

     <%= render :partial => '/shared/menu_branch', :collection => roots.select{|p|roots.rindex(p) < 4}, :locals => {
                     :hide_children => hide_children,
                     :sibling_count => (roots.length - 1),
                     :apply_css => true 
                   } -%>
    

    this is what I added.

    :collection => roots.select{|p|roots.rindex(p) < 4},