How it works
The admin navigation system works through injection points defined throughout the sidebar. You can inject custom navigation items into these predefined locations, add new top-level menu items, or create nested submenus.Navigation Injection Points
The main navigation file is located atadmin/app/views/spree/admin/shared/sidebar/_store_nav.html.erb
and provides several injection points:
Available Injection Points
store_nav_partials
store_nav_partials
store_products_nav_partials
store_products_nav_partials
store_orders_nav_partials
store_orders_nav_partials
store_settings_nav_partials
store_settings_nav_partials
settings_nav_partials
settings_nav_partials
Using the nav_item Helper
Thenav_item
helper method is provided by Spree::Admin::NavigationHelper
and makes it easy to create properly formatted navigation items.
Method Signature
Parameters
The text label for the navigation item. Can be HTML-safe content.
The URL the navigation item links to. Use the
spree.
route helper prefix.Optional icon name from Tabler Icons. The icon will be displayed before the label.
Manually set whether the link should be marked as active. If not specified, it will be auto-detected based on the current URL.
Additional data attributes to add to the link element.
Basic Usage
With Active State
With Block Content
Adding a Simple Navigation Item
Let’s add a new “Inventory” navigation item to the main sidebar.Step 1: Create the Partial
Step 2: Add Navigation Code
Always wrap your navigation items with authorization checks using
can?()
to ensure users only see menu items they have permission to access.Step 3: Register the Partial
Add this to yourconfig/initializers/spree.rb
:
Step 4: Add Translations
In yourconfig/locales/en.yml
:
Step 5: Restart Your Server
Restart your web server to load the initializer changes. The navigation item should now appear in the sidebar.Creating Nested Navigation (Submenus)
To create a navigation item with a submenu, you need to use thenav-submenu
class and manage the visibility based on the active state.
Example: Adding a Nested Menu
Key Points for Submenus
-
Active State Variable: Define a variable to track when any item in the menu group is active:
-
Parent Navigation Item: Use the active state variable for the parent item:
-
Submenu Container: Use the
nav-submenu
class and conditionally addd-none
to hide when inactive: -
Child Items: Add child navigation items within the submenu:
-
Nested Injection Point (Optional): Add an injection point within the submenu for further extensibility:
Advanced Examples
Navigation with Badge
Navigation with Complex Active Logic
Extending Existing Submenus
To add an item to an existing submenu (e.g., Products), use the appropriate injection point: Create:app/views/spree/admin/shared/_custom_products_nav.html.erb
config/initializers/spree.rb
:
Navigation in Settings Mode
When the admin is in “Settings mode” (the dedicated settings view), use thesettings_nav_partials
injection point:
Best Practices
Authorization
Always use
can?()
checks to ensure users only see navigation items they have permission to access.Translations
Use
Spree.t()
for all navigation labels to support internationalization.Icons
Use consistent icons from Tabler Icons that match Spree’s design language.
Active States
Define clear active state logic to highlight the current section in the navigation.
Route Helpers
Always use
spree.
prefixed route helpers to reference admin routes correctly.Injection Points
Add your own injection points in submenus to allow further extensions by other developers.
Common Patterns
Multiple Controller Check
Path-based Check
Controller and Action Check
Parameters-based Check
Troubleshooting
Navigation item not appearing
Navigation item not appearing
Icon not displaying
Icon not displaying
- Verify the icon name exists in Tabler Icons
- Check that you’re using the correct parameter name:
icon:
noticon_name:
- Ensure the icon name is a string, e.g.,
icon: 'boxes'
Submenu not showing/hiding properly
Submenu not showing/hiding properly
Translation missing
Translation missing
- Add the translation key to your locale file
- Ensure the locale file is in the correct location
- Restart your server after adding translations
- Check for typos in the translation key
Related Documentation
- Extending Admin UI - Learn about other UI injection points
- Helper Methods - Explore other admin helper methods
- Permissions - Understand the authorization system