-
Notifications
You must be signed in to change notification settings - Fork 462
Suckerfish
jgu edited this page May 1, 2018
·
4 revisions
If you want to add a "suckerfish dropdown" to your views, this is a starting point.
In a controller, add the following. This will get you a one level dropdown:
class AnyController < ActionController::Base
before_action :set_categories
helper_method :suckerfish
def set_categories
@categories = Category.where(ancestry: nil)
end
def suckerfish(node)
fuc = lambda do |nodes|
return "" if nodes.empty?
return "<ul>" +
nodes.inject("") do |string, (node, children)|
string + "<li rel='#{node.id}'>" +
"<a href=#{polymorphic_url(node)}>#{node.name}</a>" +
fuc.call(children) +
"</li>"
end +
"</ul>"
end
fuc.call(node.descendants.arrange)
end
end
And in your view, add something like this. It will get you close to the markup needed in those suckerfish examples:
<ul id="nav-categories">
<% @categories.each do |node| %>
<li>
<%= link_to node.name, node %>
<%= suckerfish(node).html_safe %>
</li>
<% end %>
</ul>
For Rails 3 & 4, if you want to use this method in views as a helper_method :suckerfish
add view_context
before link_to
:
view_context.link_to(node.name, node.path) + # don't forget to set your path