Rails Admin is a Gem that creates an interface for you to manage your data. For further explanation take a look at the Gem’s page on GitHub!
Start by:
On your gemfile: gem 'rails_admin'
Run bundle install
Restart your Rails server.
Run rails generate rails_admin:install
Create an admin user with rails g devise Admin
Create a namespace under your routes:
https://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing for how to do that!
We will instead follow the rails admin installation guide by adding this line to routes.rb
below devise_for :admin
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
Authenticate the admin view by uncommenting and replace user
with admin
in config/initializers/rails_admin.rb
config.authenticate_with do
warden.authenticate! scope: :admin
end
config.current_user_method(&:current_admin)
http://localhost:3000/admin Check out your tables!
It's really complete! You can create data, update data, and delete data in all of your tables
Let's limit the access to certain models using admin!
In rails_admin.rb
config.actions do
edit do
only ['post', 'user']
end
delete do
except ['like']
end
end
Re run your admin see what changed!
Put something like this this in your model class (be sure to rename things appropriately):
class Post < ActiveRecord::Base
belongs_to :user
has_many :likes
has_many :users_liked, through: :likes, source: :user
validates :caption, presence: true
rails_admin do
configure :user do
label 'Owner of this post: '
end
end
end