ソフトウェア/rails/devise+権限管理
ユーザー&権限管理の定番らしいです†
devise の設定†
参考:http://easyramble.com/check-list-for-rails-devise.html
gem のインストール†
参考:https://qiita.com/iamdaisuke/items/79d60b3c23e465ae6460
Gemfile に追加
gem 'devise'
gem 'devise-i18n'
gem 'rails-i18n'
$ bundle install
$ sprint stop
$ rails g devise:install
create config/initializers/devise.rb
create config/locales/devise.en.yml
===============================================================================
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:
<%= notice %>
<%= alert %>
4. You can copy Devise views (for customization) to your app by running:
rails g devise:views
===============================================================================
$ rails g devise user
$ rake db:migrate
i18n の付いたのを入れないと
MissingTranslationData in Devise::Registrations#new
というエラーが出て困る。
参考:
http://jewelrybox.wpblog.jp/2017/09/05/active_adminでの-i18nmissingtranslationdata-のエラーの対処/
http://jewelrybox.wpblog.jp/2017/08/08/devise導入と日本語化-rails/
https://github.com/tigrish/devise-i18n
sprint stop をしないと rails g devise:install が帰ってこない。
参考:http://ohara.geniusroots.com/entry/2017/03/26/145253
ユーザー名を追加する†
参考:http://easyramble.com/add-field-devise-signup-form.html
$ rails generate migration AddNameToUsers name:string
$ jed db/migrate/***_add_name_to_users.rb
class AddNameToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string, null: false, default: ''
add_index :users, :name, unique: true # ← 必要に応じて
end
end
$ bundle exec rake db:migrate
$ rails g devise:i18n:views
app/views/devise/registrations/new.html.erb
app/views/devise/registrations/edit.html.erb
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </div>
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
+ before_action :configure_permitted_parameters, if: :devise_controller?
protected
+ def configure_permitted_parameters
+ devise_parameter_sanitizer.for(:sign_up) << :name
+ end
end
app/models/user.rb
class User < ActiveRecord::Base
...
+ validates :name, length: { minimum: 3, maximum: 50 }
...
end