I'm trying to create a sidebar menu with the below information:
# app/controllers/users_controller.rb
def show
@user = User.friendly.find(params[:id])
end
# config/routes.rb
get "/user/:id", to: "users#show"
I have the link from the dropdown menu to the current_user path:
<!-- app/views/layouts/_dropdown_menu.html.erb -->
<%= link_to "My Account", current_user %>
This is how I create the sidebar menu:
<!-- app/views/layouts/dashboard.html.erb -->
<%= render "shared/sidebar_panel" %>
<!-- app/views/shared/_sidebar_panel.html.erb -->
<%= render "shared/nav" %>
<!-- app/views/shared/_nav.html.erb -->
<% SidebarMenu.all.each do |entry| %>
<% if entry[:group_title].present? %>
<li class="nav-title"><%= entry[:group_title]%></li>
<% end %>
<%= render partial: 'shared/nav_submenu',
collection: entry[:children],
as: :sub_menu,
locals: {parents: []} %>
<% end %>
<!-- app/views/shared/_nav_submenu.html.erb -->
<li>
<a href="<%= sub_menu[:href] %>">
<span><%= sub_menu[:title] %></span>
<% if sub_menu[:subtitle].present? %>
<span class="<%= sub_menu[:subtitle_class] %>">
<%= sub_menu[:subtitle] %>
</span>
<% end %>
</a>
<% if sub_menu[:children].present? %>
<ul>
<%= render partial: 'shared/nav_submenu',
collection: sub_menu[:children],
as: :sub_menu,
locals: {parents: parents + [sub_menu]} %>
</ul>
<% end %>
</li>
I have a SidebarMenu model and I've added current_user path:
# app/models/sidebar_menu.rb
class SidebarMenu
class << self
include Rails.application.routes.url_helpers
end
def self.all
[
{
group_title: "Account & Contact",
children: [
{
href: "#",
title: "Account",
icon: "...",
children: [
{
href: current_user,
title: "My Account"
}
]
},
# ...
]
}
]
end
end
But it raises an error message:
undefined local variable or method `current_user' for SidebarMenu:Class
Can anyone advise me how I can fix this?