blue workbench

Ruby and Rails

【PostgreSQL】pgcliを導入する

インストールする

$ brew install pgcli

軽く触ってみる

twitter_copy_development というDBがある前提。

$ pgcli twitter_copy_development
Server: PostgreSQL 12.2
Version: 3.0.0
Chat: https://gitter.im/dbcli/pgcli
Home: http://pgcli.com
twitter_copy_development> select * from tweets;
+------+-----------+--------+----------------------------+----------------------------+
| id   | user_id   | body   | created_at                 | updated_at                 |
|------+-----------+--------+----------------------------+----------------------------|
| 1    | 1         | あいう | 2020-04-19 10:18:05.864627 | 2020-04-19 10:18:05.864627 |
+------+-----------+--------+----------------------------+----------------------------+
SELECT 1
Time: 0.018s

自動保管がすごく便利です。

MySQL版もある

ちなみにmycliというMySQL版もあります。

$ brew install mycli

参考

【Ruby】ローカルPCのRubyバージョンを最新化する

公式サイトで最新バージョンを確認する

https://www.ruby-lang.org/ja/downloads/ にアクセスして、最新バージョンを確認します。

現バージョンを確認する

$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin17]

rbenv、ruby-build を最新化する

$ brew update
$ brew upgrade rbenv ruby-build
$ rbenv install -l

欲しいバージョンがなければ以下を実行する。

$ cd ~/.rbenv/plugins/ruby-build
$ git pull
$ rbenv install -l

新しいバージョンをインストールする

$ rbenv install 2.7.1
$ rbenv global 2.7.1

更新後のバージョンを確認する

$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin17]

参考

qiita.com

【Rails】RailsアプリケーションのRubyバージョンを上げる

1. ローカルPCのRubyバージョンを上げる

now-on-air.hatenablog.com

2. アプリケーションの .ruby-version を更新する

$ cd プロジェクトルート
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
$ rbenv local 2.7.1
$ ruby -v          
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin17]

.ruby-version も更新されます。

3. gemを更新する

$ rails s
rbenv: rails: command not found

The `rails' command exists in these Ruby versions:
  2.3.3
  2.5.1
  2.6.5
  2.7.0

$ gem update --system
Latest version already installed. Done.

→不要かも?

$ gem install bundler
Fetching bundler-2.1.4.gem
Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Installing ri documentation for bundler-2.1.4
Done installing documentation for bundler after 3 seconds
1 gem installed

→不要かも?

$ rails s
rbenv: rails: command not found

The `rails' command exists in these Ruby versions:
  2.3.3
  2.5.1
  2.6.5
  2.7.0

$ bundle install
Traceback (most recent call last):
        2: from /Users/y_tsubasa/.rbenv/versions/2.7.1/bin/bundle:23:in `<main>'
        1: from /Users/y_tsubasa/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems.rb:294:in `activate_bin_path'
/Users/y_tsubasa/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems.rb:275:in `find_spec_for_exe': Could not find 'bundler' (1.16.5) required by your /Users/y_tsubasa/repo/tsubasa-yoshida/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:1.16.5`

$ bundle update --bundler
Your Ruby version is 2.7.1, but your Gemfile specified 2.5.1

(ここでGemfileのruby 'x.x.x'を修正する)

$ bundle update --bundler

$ bundle install

→不要かも?

$ bundle update

$ rails s
=> Booting Puma
=> Rails 5.2.4.2 application starting in development 
=> Run `rails server -h` for more startup options
/Users/y_tsubasa/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/y_tsubasa/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/actionpack-5.2.4.2/lib/action_dispatch/middleware/static.rb:111: warning: The called method `initialize' is defined here
Puma starting in single mode...
* Version 3.12.4 (ruby 2.7.1-p83), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

参考

qiita.com

【Rails/devise】ルーティングをカスタマイズしたかったので、いろいろ実験する

ノーマル

Rails.application.routes.draw do
  devise_for :users
end
$ rails routes
                               Prefix Verb   URI Pattern                                                                              Controller#Action
                     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
                new_user_confirmation GET    /users/confirmation/new(.:format)                                                        devise/confirmations#new
                    user_confirmation GET    /users/confirmation(.:format)                                                            devise/confirmations#show
                                      POST   /users/confirmation(.:format)                                                            devise/confirmations#create

結局こうした

Rails.application.routes.draw do
  # ルーティングをカスタマイズする場合は、まず skip でルーティング生成をスキップする
  devise_for :users, skip: [:sessions, :registrations], controllers: {
      registrations: 'users/registrations'
  }

  # skip したルーティングをここでカスタマイズする
  devise_scope :user do
    get 'sign_in', to: 'devise/sessions#new', as: :new_user_session
    post 'sign_in', to: 'devise/sessions#create', as: :user_session
    delete 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session

    # コントローラをカスタマイズした場合は、以下もカスタマイズしたほうを見るようにする
    get 'sign_up', to: 'users/registrations#new', as: :new_user_registration
    post 'sign_up', to: 'users/registrations#create', as: :user_registration
  end
end
$ rails routes
                               Prefix Verb   URI Pattern                                                                              Controller#Action
                    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
                new_user_confirmation GET    /users/confirmation/new(.:format)                                                        devise/confirmations#new
                    user_confirmation GET    /users/confirmation(.:format)                                                            devise/confirmations#show
                                      POST   /users/confirmation(.:format)                                                            devise/confirmations#create
                     new_user_session GET    /sign_in(.:format)                                                                       devise/sessions#new
                         user_session POST   /sign_in(.:format)                                                                       devise/sessions#create
                 destroy_user_session DELETE /sign_out(.:format)                                                                      devise/sessions#destroy
                new_user_registration GET    /sign_up(.:format)                                                                       users/registrations#new
                    user_registration POST   /sign_up(.:format)                                                                       users/registrations#create

コントローラをカスタマイズする

Rails.application.routes.draw do
  devise_for :users, controllers: {
      registrations: 'users/registrations'
  }
end
$ rails routes
                               Prefix Verb   URI Pattern                                                                              Controller#Action
                     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)                                                                  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
                new_user_confirmation GET    /users/confirmation/new(.:format)                                                        devise/confirmations#new
                    user_confirmation GET    /users/confirmation(.:format)                                                            devise/confirmations#show
                                      POST   /users/confirmation(.:format)                                                            devise/confirmations#create

スキップする

Rails.application.routes.draw do
  devise_for :users, skip: [:sessions, :registrations]
end
$ rails routes
                               Prefix Verb   URI Pattern                                                                              Controller#Action
                    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
                new_user_confirmation GET    /users/confirmation/new(.:format)                                                        devise/confirmations#new
                    user_confirmation GET    /users/confirmation(.:format)                                                            devise/confirmations#show
                                      POST   /users/confirmation(.:format)                                                            devise/confirmations#create

【Rails】Simple Formをやる

導入まで

Gemfile

gem 'simple_form'
$ bundle install
$ rails g simple_form:install
Running via Spring preloader in process 2486
SimpleForm 3 supports Bootstrap and Zurb Foundation 5. If you want a configuration that is compatible with one of these frameworks, then please re-run this generator with --bootstrap or --foundation as an option.
      create  config/initializers/simple_form.rb
       exist  config/locales
      create  config/locales/simple_form.en.yml
      create  lib/templates/haml/scaffold/_form.html.haml

参考

【Rails】タイムゾーン、ロケールを設定する

config/application.rb

config.time_zone = 'Tokyo'
config.active_record.default_timezone = :local

config.i18n.default_locale = :ja

Gemfile

group :development do
  gem 'i18n_generators'
end
$ bundle install
$ rails g i18n_locale ja
Running via Spring preloader in process 1833
fetching ja.yml from rails-i18n repository...
      create  config/locales/ja.yml

参考

qiita.com

qiita.com

qiita.com