1

In my Rails app, I have several controllers that makeup a site for my client. With end users and Admins using virtually all of the controllers.

I have created a partial for including navigation links. My problem is that I don't want them to appear inside of the admin section. The part that makes this tricky is that while the navigation should appear for things like pages/home, /galleries or /galleries/5 they should not appear for things like /pages/new or /galleries/5/edit.

Up until now I have been adding logic to application.html.erb, but things are beginning to get out of hand, there are so many rules in my if statement that I feel dirty about it. Here is an example:

snippet from application.html.erb

<% unless current_page?('/') or current_page?('/admin') or current_page?({action: :new}) %>
    <%= render "layouts/top_links" %>
<% end %>

This is all I have written so far, but it will almost certainly get even longer if I continue this way. So here are my questions:

  1. Where should I put the logic for this and how should I call it?
  2. Is current_page? the best way to lay this out?
drewwyatt
  • 5,989
  • 15
  • 60
  • 106

3 Answers3

3

You can put such dirty logic into helper to make it cleaner

# layouts/application.html.erb
nav_links

# application_helper
def nav_links
  return "" if action_name.in?['new', 'edit'] || controller_path == 'admin'
  render partial: 'layouts/nav'
end

Done.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
1

You could advance with a role based approach, as it is the most widely used method for access control in rails. Indeed, all those if-else loops in application.html.erb would be a little bit of code smell anyway.

Check Ryan Bates CanCan gem for this purpose.

QuestionEverything
  • 4,809
  • 7
  • 42
  • 61
  • I really wish this would work. And it may be what I end up doing anyway. Here is my issue with this approach, though: If an admin visits the site without logging out, I still want the links to appear. The conditions should be based on location, not user. Does that make sense? – drewwyatt Sep 01 '13 at 05:58
  • that does, but you have described the problem associated with it yourself. if you can manage it, then there is no problem at all! – QuestionEverything Sep 01 '13 at 06:02
1

One possibility is to make use of before_filter, which you can set up in your ApplicationController. What you can do

Apply your own flavour to this, but a goofy example might be:

class ApplicationController < ActionController::Base
    before_filter :set_navigation_visibility

    def set_navigation_visibility
        # Your custom logic
    end

That method can also be overridden in the subcontrollers if necessary, or modified using the :only and :except options.

Another possibility is making use of a role-based access-control system. There are many, one such library I have used is Simple-Navigation. A google search for role-based access control for rails seems to yield a number of results that might be helpful.

Paul Richter
  • 10,908
  • 10
  • 52
  • 85