I'm trying to build a Rails helper that will create a nested dropdown menu containing links where the top most is either 'All' or the current param and the dropdown contains the other options excluding the current param if there is one.
So for example if I have no post_type param I would see:
<ul>
<li><a href="">All</a>
<ul>
<li><a href="">Discussions</a></li>
<li><a href="">Snaps</a></li>
<li><a href="">Code</a></li>
<li><a href="">Links</a></li>
</ul>
</li>
</ul>
But if I had a post_type param of 'discussion' then I would see:
<ul>
<li><a href="">Discussions</a>
<ul>
<li><a href="">All</a></li>
<li><a href="">Snaps</a></li>
<li><a href="">Code</a></li>
<li><a href="">Links</a></li>
</ul>
</li>
</ul>
In my view I have:
<ul class="filter-menu">
<li>
<%= current_post_type %>
<ul class="filter-menu__drop-down">
<%= list_post_types %>
</ul>
</li>
</ul>
And in my Helper I have the following:
post_types = {
:id => 'All post types',
:icon => 'icon-file-text2',
:link => posts_path(:filter => params[:filter], :time => params[:time])
}, {
:text => 'Discussions',
:icon => 'icon-bubbles2',
:path => posts_path(:filter => params[:filter], :post_type => 'discussions', :time => params[:time])
}, {
:id => 'Snaps',
:icon => 'icon-images',
:link => posts_path(:filter => params[:filter], :post_type => 'snaps', :time => params[:time])
}, {
:id => 'Code',
:icon => 'icon-embed2',
:link => posts_path(:filter => params[:filter], :post_type => 'code', :time => params[:time])
}, {
:id => 'Links',
:icon => 'icon-link',
:link => posts_path(:filter => params[:filter], :post_type => 'links', :time => params[:time])
}
def current_post_type
if params[:post_type].present? # && check if params is present and matches above
post_type = params[:post_type].downcase
link_to raw('<i class="' + post_types[post_type][:icon] + '"></i> ' + post_types[post_type][:text] + ' <span class="chevron">▾</span>'), post_types[post_type][:link]
else
link_to raw('<i class="' + post_types.first[:icon] + '"></i> ' + post_types.first[:text] + ' <span class="chevron">▾</span>'), post_types.first[:link]
end
end
def list_post_types
post_types.each do |post_type| # need to exclude any that match current post_type
link_to raw('<i class="' + post_types[post_type][:icon] + '"></i> ' + post_types[post_type][:text] + ''), post_types[post_type][:link]
end
end
The first issue I'm having is that the helper doesn't see the params... Do I have to pass params directly to the helper? e.g. current_post_type(params)
Second, how can I loop through my post_types
variable to pull the correct data across to build the links. post_types[post_type][:icon]
doesn't seem to be working.
Aucun commentaire:
Enregistrer un commentaire