-
-
Save francelwebdev/b1f147affd62a558897936305aaf223c to your computer and use it in GitHub Desktop.
"Best Practices For Building A Rails Admin Interface From Scratch" sample setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # app/views/layouts/admin.html.erb %> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Admin Interface</title> | |
<%= csrf_meta_tags %> | |
<% # Optionally use admin-specific assets here instead of the normal application assets %> | |
<%= stylesheet_link_tag 'admin', media: 'all', 'data-turbolinks-track': 'reload' %> | |
<%= javascript_include_tag 'admin', 'data-turbolinks-track': 'reload' %> | |
</head> | |
<header> | |
<h1>Welcome Admin User</h1> | |
</header> | |
<body> | |
<%= yield %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/admin/articles_controller.rb | |
# Note the namespacing and the inheritance | |
class Admin::ArticlesController < Admin::BaseController | |
def index | |
@articles = Article.all | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/assets.rb | |
# ... | |
# Precompile additional assets. | |
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added. | |
Rails.application.config.assets.precompile += %w( admin.js admin.css ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/admin/base_controller.rb | |
# Inherit directly from ActionController::Base rather than ApplicationController to ensure clean separation | |
class Admin::BaseController < ActionController::Base | |
# use an admin-specific layout instead of the main application layout | |
layout "admin" | |
# all child controllers will automatically enforce access to admins only | |
before_action :require_admin | |
def require_admin | |
# ... | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
Rails.application.routes.draw do | |
namespace :admin do | |
resources :articles | |
end | |
# ... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment