ユーザ管理が出来るようにします。
「GemFile」に以下の記述を追加します。
GemFile(78行目)
gem 'trestle-search', '~> 0.4.2' gem 'trestle-active_storage', '~> 3.0' gem "mini_magick" gem 'image_processing', '~> 1.2'
GemFile
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.6' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.3', '>= 6.0.3.1' # Use postgresql as the database for Active Record gem 'pg', '>= 0.18', '< 2.0' # Use Puma as the app server gem 'puma', '~> 4.1' # Use SCSS for stylesheets gem 'sass-rails', '>= 6' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 4.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '~> 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] # Bulma gem 'bulma-rails', '~> 0.7.4' gem 'bulma-extensions-rails', '~> 1.0.30' #デバイス gem 'devise' # 日本語化 gem 'rails-i18n' # google認証 gem 'omniauth' gem 'omniauth-google-oauth2' # アマゾンS3 gem "aws-sdk" #管理ダッシュボード gem 'trestle', '~> 0.9.3' gem 'trestle-auth', '~> 0.4.0' # trestle検索 gem 'trestle-search', '~> 0.4.2' # trestle画像アップロードと表示 gem 'trestle-active_storage', '~> 3.0' gem "mini_magick" gem 'image_processing', '~> 1.2'
コマンド
bundle
ユーザテーブルに「有効化」と「無効化」を切り替えできるフィールドを作成します。
コマンド
rails g migration AddActiveToUser active:boolean
「db\migrate\20200802081937_add_active_to_user.rb」ファイルを編集します。
記述更新 db\migrate\20200802081937_add_active_to_user.rb
3行目に「, default: true」の記述を追加しています。
class AddActiveToUser < ActiveRecord::Migration[6.0] def change add_column :users, :active, :boolean, default: true end end
コマンド マイグレーション適用
rails db:migrate
「app\admin」フォルダに「users_admin.rb」ファイルを新規作成します。
app\admin\users_admin.rb(新規作成したファイル)
Trestle.resource(:users) do remove_action :new remove_action :destroy menu do item :登録ユーザー, icon: "fa fa-user" end table do column :full_name column :email column :active column :created_at, align: :center actions do |toolbar, instance, admin| toolbar.link '有効化', admin.path(:activate, id: instance.id), method: :post, class: 'bg-success' toolbar.link '無効化', admin.path(:deactivate, id: instance.id), method: :post, class: 'bg-danger' end end controller do def activate user = admin.find_instance(params) user.update(active: true) flash[:message] = "ユーザが有効化されました" redirect_to admin.path(:show, id: user) end def deactivate user = admin.find_instance(params) user.update(active: false) flash[:message] = "ユーザが無効化されました" redirect_to admin.path(:show, id: user) end end routes do post :activate, on: :member post :deactivate, on: :member end form do |user| text_field :full_name text_field :email active_storage_field :avatar end search do |query| if query User.where("email ILIKE ? OR full_name ILIKE ?", "%#{query}%", "%#{query}%") else User.all end end active_storage_fields do [:avatar] end end
ブラウザ確認
http://localhost:3000/admin/users
登録ユーザの管理ができるようになりました。
アバター画像のアップロードもできます。