I really only want to see projects and users on the left. But each model has a bunch of relationships. I find if I remove the dashboard file, I can remove the option in the menu, but then I cannot display that relationship when viewing a project or user.
I'm using the following solution.
Create file app/views/administrate/application/_sidebar.html.erb which contains the code:
<ul class="sidebar__list">
<% Administrate::Namespace.new(namespace).resources.each do |resource| %>
<% unless [:photos].include? resource %>
<li>
<%= link_to(
display_resource_name(resource),
[namespace, resource],
class: "sidebar__link sidebar__link--#{nav_link_state(resource)}"
) %>
</li>
<% end %>
<% end %>
</ul>
Here you can see hardcoded array [:photos]. It is array of resource names to exclude from sidebar.
I do a similar solution
class ServiceObject
# set pages which you wanna remove from sidebar
HIDDEN_PAGES = [:hide_me_from_sidebar].freeze
def self.routes(admin)
admin.resources.reject { |i| i.in?(HIDDEN_PAGES) }
end
end
admin/application/_sidebar.html.erb
<ul class="sidebar__list">
<% resources = ServiceObject.routes(Administrate::Namespace.new(namespace)) %>
<% resources.each do |resource| %>
<li>
<%= link_to(
display_resource_name(resource),
[namespace, resource],
class: "sidebar__link sidebar__link--#{nav_link_state(resource)}"
) %>
</li>
<% end %>
</ul>
Is there a good way to re-order them?
@tehfailsafe I believe the order of the resources in the routes.rb will accomplish that pretty easily. There are likely other options, but I used that approach successfully.
Hah! Perfect, didn't even think to re-order them...
Sounds like this one is resolved.
FYI - there's now a generator you can use to customize the sidebar
rails g administrate:views:sidebar
Hope that helps!
For future Googlers, the generator is now rails g administrate:views:navigation.
Also now, @ID25's service object should be
class ServiceObject
# set pages which you wanna remove from sidebar
HIDDEN_PAGES = [:hide_me_from_sidebar].freeze
def self.routes(admin)
admin.resources.reject { |i| i.resource.in?(HIDDEN_PAGES) }
end
end
To get this working, I had to switch HIDDEN_PAGES from an array of symbols, to an array of strings:
class ServiceObject
# set pages which you wanna remove from sidebar
HIDDEN_PAGES = ["hide_me_from_sidebar"].freeze
def self.routes(admin)
admin.resources.reject { |i| i.resource.in?(HIDDEN_PAGES) }
end
end
Very useful, thanks!
@FanaHOVA Do you think you'd be able to open a PR documenting that?
@nickcharlton Sorry I just got back home from a long weekend. Opened a PR: https://github.com/thoughtbot/administrate/pull/1106
I am pretty new to Rails and I had trouble understanding the exact steps to implement the feature mentioned here: I did not even know that Service Objects existed in Ruby.
For newbies like me, here are the exact steps:
services in app.app/services, create a file called admin_navigation.rb.admin_navigation.rb:class AdminNavigation
# Set pages to remove from Admin Dashboard's sidebar
HIDDEN_PAGES = %w[page1 page2 page3 page4].freeze
def self.routes(admin)
admin.resources.reject { |i| i.resource.in?(HIDDEN_PAGES) }
end
end
app\views\admin\application\_navigation.html.erb, change the code to:<nav class="navigation" role="navigation">
<% resources = AdminNavigation.routes(Administrate::Namespace.new(namespace)) %>
<% resources.each do |resource| %>
<%= link_to(
display_resource_name(resource),
[namespace, resource_index_route_key(resource)],
class: "navigation__link navigation__link--#{nav_link_state(resource)}"
) %>
<% end %>
</nav>
I hope this will help!
You can remove resources form the sidebar by removing their index action from the routes. For example:
Rails.application.routes.draw do
namespace :admin do
resources :line_items, except: :index
resources :orders
resources :products
root to: "customers#index"
end
end
In this case, only Orders and Products will appear in the sidebar, while Line Items can still appear as an association.
Is that what you are trying to achieve?
Hi @pablobm, i am using 0.14.0 version of the gem and you suggested solution does not work for me. I need to hide a menu item but adding the except: :index does not hide it.
@aksharj - That might be a bug. However: in other issues you mention having custom templates. Perhaps you have a custom _navigation.html.erb that performs outdated behaviour? Could you re-generate it from the original (using rails g administrate:views:navigation) and see if it needs changes?
@pablobm There's one problem with this solution, at this time after you delete a relation administrate redirects to the relation's index page. And without an index page in routes it'll just fail with 500 error.
But it's probably a different problem, and the redirect should not be to an index page, but back. Like, if you delete line_item from the order's show page, you should be redirected back to the order's show page, not to line_item index page.
Uh, that's a good point @haukot. Do you think you could provide a PR to fix that behaviour?
@haukot , @pablobm , I would like to work on it, can I?
@edimossilva I don't stop you :) I won't do that at least in a month. We have done a workaround in the project.
@edimossilva, go for it.
Most helpful comment
For future Googlers, the generator is now
rails g administrate:views:navigation.