[RUBY] [Active Admin] rails g active_admin: Create model management screen by resource model name

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:

environment

What is ActiveAdmin?

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:

--ActiveAdmin Site

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.

ActiveAdmin installation procedure

--Add the gems ʻactiveadmin and deviseto the Gemfile and install the gem withbundle 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.

スクリーンショット 2020-06-05 12.55.46.png

--Login with the following ID and password

UserID: [email protected]
Password: password

--Then, the management screen created by ActiveAdmin will be displayed.

スクリーンショット 2020-06-05 17.34.24.png

rails g active_admin: resource model name

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. スクリーンショット 2020-06-05 13.57.33.png

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. スクリーンショット 2020-06-05 18.02.07.png

At the end

ActiveAdmin has many other features, so check out the official website and other Qiita articles to learn more: sunny:

Recommended Posts

[Active Admin] rails g active_admin: Create model management screen by resource model name
Cannot create user from Active Admin admin screen
Delete Active Admin resource
rails g model Overall flow
Rails singular resource routing by resource