Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.