Nice to meet you. I'm Yori-Goreng, who continues to study Ruby and Rails at an online programming school: relaxed: This time, I will introduce a part of management screen creation using ActiveAdmin: sunny :: sunny:
ActiveAdmin is a gem that creates an administration screen in Ruby on Rails. ** From the management screen, you can easily create, update, and delete data. ** ** It's so convenient that you can't go wrong with it: relaxed:
Description quote: Active Admin is a Ruby on Rails plugin for generating administration style interfaces. It abstracts common business application patterns to make it simple for developers to implement beautiful and elegant interfaces with very little effort.
--Add the gems ʻactiveadmin and
deviseto the Gemfile and install the gem with
bundle install`.
gem 'activeadmin'
gem 'devise'
--Run rails g active_admin: install
. The following text will be displayed in the terminal.
===============================================================================
Some setup you must do manually if you haven't yet:
1.Ensure you have defined default url options in your environments files. Here
is an example of default_url_options appropriate for a development environment
in config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
In production, :host should be set to the actual host of your application.
2. Ensure you have defined root_url to *something* in your config/routes.rb.
For example:
root to: "home#index"
3. Ensure you have flash messages in app/views/layouts/application.html.erb.
For example:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
4.You can copy Devise views (for customization) to your app by running:
rails g devise:views
===============================================================================
--Create a model to be managed on the management screen by rails g model model name
. As an example, run rails g model text
.
--Modified the migration file created at the same time as the model and added the required columns.
db/migrate/20200605042220_create_texts.rb
class CreateTexts < ActiveRecord::Migration[5.2]
def change
create_table :texts do |t|
#This time, title,Let's add two columns called content.
t.string :title
t.text :content
t.timestamps
end
end
end
--Migrate with rails db: migrate
.
--Inject user's initial data into the database with rails db: seed
.
--Start the server with rails s
.
--Access http: // localhost: 3000 / admin and display the login screen.
--Login with the following ID and password
UserID: [email protected]
Password: password
--Then, the management screen created by ActiveAdmin will be displayed.
This command creates a management menu for the model specified by the model name.
This time, I want to add the management menu of the text model created earlier, so execute rails g active_admin: resource text
.
This will create texts.rb
in app / admin and add a Texts menu to the admin screen. From this screen, you can update the model data.
By the way, it seems that the data cannot be updated without adding permit_params: column name
to texts.rb
.
app/admin/texts.rb
ActiveAdmin.register Text do
permit_params :title, :content
end
I will try to add data. Launch the console with rails c
and execute the create action ⇒model name.create (column name: "column value")
rails c
Text.create(title: "1", content: "hogehoge")
It was added safely. You can easily modify the data with View, Edit, Delete on the right side of the screen.
ActiveAdmin has many other features, so check out the official website and other Qiita articles to learn more: sunny: