bundle exec rails sで、いつもの「Yay! You’re on Rails!」が確認できるところまで進めます。
root 用のコントローラとビューを作成
以下のコマンドで、root に割り当てるコントローラとビューを作成。
1
bundle exec rails g controller home index --skip-test-framework --skip-assets
root を変更
作成したhome/indexを root に変更。
config/routes.rb(変更前)
1 2 3 4
Rails.application.routes.draw do get 'home/index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
config/routes.rb(変更後)
1 2 3
Rails.application.routes.draw do root to:'home#index' end
localhost:3000にアクセスして以下の表示を確認。
gem インストール
以下の様に Gemfile に追記する。
Gemfile
1 2
gem 'devise' gem 'devise-i18n'
devise をインストール。
1 2 3
bundle install bundle exec rails g devise:install #忘れがち注意
Prefix Verb URI Pattern Controller#Action new_admin_session GET /admins/sign_in(.:format) devise/sessions#new admin_session POST /admins/sign_in(.:format) devise/sessions#create destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy new_admin_password GET /admins/password/new(.:format) devise/passwords#new edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit admin_password PATCH /admins/password(.:format) devise/passwords#update PUT /admins/password(.:format) devise/passwords#update POST /admins/password(.:format) devise/passwords#create new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create
Prefix Verb URI Pattern Controller#Action new_admin_session GET /admins/sign_in(.:format) admins/sessions#new admin_session POST /admins/sign_in(.:format) admins/sessions#create destroy_admin_session DELETE /admins/sign_out(.:format) admins/sessions#destroy new_admin_password GET /admins/password/new(.:format) admins/passwords#new edit_admin_password GET /admins/password/edit(.:format) admins/passwords#edit admin_password PATCH /admins/password(.:format) admins/passwords#update PUT /admins/password(.:format) admins/passwords#update POST /admins/password(.:format) admins/passwords#create new_user_session GET /users/sign_in(.:format) users/sessions#new user_session POST /users/sign_in(.:format) users/sessions#create destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy new_user_password GET /users/password/new(.:format) users/passwords#new edit_user_password GET /users/password/edit(.:format) users/passwords#edit user_password PATCH /users/password(.:format) users/passwords#update PUT /users/password(.:format) users/passwords#update POST /users/password(.:format) users/passwords#create cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit user_registration PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy POST /users(.:format) users/registrations#create
admin と user のすべてのアクションが、admins か users の配下になった。
devise のビューを作成
次のコマンドでビューを作成。
1 2
bundle exec rails g devise:views users bundle exec rails g devise:views admins
= form_for @admins_food do |f| - if @admins_food.errors.any? #error_explanation h2 = "#{pluralize(@admins_food.errors.count, "error")} prohibited this admins_food from being saved:" ul - @admins_food.errors.full_messages.each do |message| li = message
.actions = f.submit
app/views/admins/foods/_form.html.slim(変更後)
1 2 3 4 5 6 7 8 9 10 11 12
= form_for [:admins,@admins_food] do |f| - if @admins_food.errors.any? #error_explanation h2 = "#{pluralize(@admins_food.errors.count, "error")} prohibited this admins_food from being saved:" ul - @admins_food.errors.full_messages.each do |message| li = message .field = f.label :name = f.text_field :name