Skip to content

Instantly share code, notes, and snippets.

@josephrexme
Created December 23, 2014 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephrexme/04a8740950bc3988c1f3 to your computer and use it in GitHub Desktop.
Save josephrexme/04a8740950bc3988c1f3 to your computer and use it in GitHub Desktop.
How I display a list of child departments on my dashboard index
class DashboardController < ApplicationController
before_action :require_login
before_filter :init
def index
@departments.each do |department|
childfetch = Department.where(parent: "#{department.name}").select('name')
instance_variable_set("@#{department.name}Children".gsub(' ','_'), childfetch.map { |child| "#{child.name}" }.join(',') )
end
end
def office
end
def preferences
end
def company
end
private
def require_login
unless session[:user_id].present?
redirect_to root_url
end
end
def init
@departments = Department.where(parent: '')
end
end
module DashboardHelper
def instancevars(arg)
instance_variable_get("@#{arg}Children".gsub(' ','_'))
end
def nameslug(arg)
arg.downcase[0,5]
end
end
<ul class="accord-style">
<% @departments.each do |department| %>
<li <%= instancevars(department.name).present? ? "data-accord=#{nameslug(department.name)}":'' %>>
<%= "#{department.name}" %>
</li>
<% if(instancevars(department.name).present?) %>
<ul id="<%= nameslug(department.name) %>">
<% instancevars(department.name).split(',').each do |child| %>
<li><a href=""><%= "#{child}" %></a></li>
<% end %>
</ul>
<% end %>
<% end %>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment