pyenvを使ってインストールしたPythonでNo module named '_tkinter'となったとき
pyenvを使ってインストールしたPythonで、tkinterを使おうとしたとき、エラーとなりました。
import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'
tkinterが自分の環境にインストールされているか調べてみました。
インストールされていないようなので、tkinterをインストールします。
$ python -m tkinter import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'
home brewを使って、tcl-tkをインストールします。
$ brew install pyenv
pyenvがtcl-tkを使うように、python-buildスクリプトを修正します。
$ cd /usr/local/Cellar/pyenv/1.2.15/plugins/python-build/bin/python-build $ cp python-build python-build_original $ vi python-build
以下のように修正します。
【修正前】 $CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1 【修正後】 $CONFIGURE_OPTS --with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
pyenvを使って、Pythonをインストールします。
$ pyenv install 3.7.5 $ pyenv global
tkinterが使えるか、テストしてみます。
$ python -m tkinter -c 'tkinter._test()'
小さな画面が表示されれば、成功です。
Active Storageを利用する準備をする
Active Storageで画像のサイズを変換する機能を利用するために、ImageMagickをインストールします。
$ brew install imagemagick
RailsでImageMagicを利用するために、Gemパッケージをインストールします。
Gemfileに以下を追加します。
gem 'mini_magick'
Gemパッケージをインストールします。
$ bundle install
$ bin/rails active_storage:install $ bin/rails db:migrate
jQueryをインストールする
Gemfileに以下を追加します。
gem 'jquery-rails'
Gemパッケージをインストールします。
$ bundle install
app/assets/javascriptsディレクトリのapplication.jsを変更します。
//= require rails-ujs //= require activestorage //= require turbolinks //= require jquery //= require_tree .
ページネーション機能を使う
Gemfileに以下を追加します。
gem "kaminari" gem "kaminari-i18n"
kaminariとkaminari-i18nをインストールします。
$ bundle install
kaminariをインストールすると、モデルのメソッドにpageメソッドが追加されます。
@users = User.page(5) @users = User.page(5).per(25)
Rails ユーザーのパスワードを保存するモデルクラス を作成する
Gemfileのbcryptのコメントを外します。
gem 'bcrypt', '~> 3.1.7'
bcryptをインストールします。
$ bundle install
Userモデルクラスを作成します。
$ bin/rails g model users
Userモデルクラスに、has_secure_password を追加します。
class User < ApplicationRecord has_secure_password end
マイグレーションファイルに、テーブルの項目を追加します。
このとき、暗号化したパスワードを保存する password_digest を追加します。
class CreateUsers < ActiveRecord::Migration[5.2] def change create_table :users do |t| t.string :email, null: false t.string :password_digest t.timestamps end end end
テーブルを作成します。
$ bin/rails db:migrate
railsコンソールを起動し、ユーザーデータを登録します。
$ bin/rails c > user = User.new > user.name = 'hikotaro" > user.email = 'hikotaro@example.co.jp' > user.password = 'zaq12wsx' > user.password_confirmation = 'zaq12wsx' > user.save
続けて、ユーザーのパスワード認証を試します。
> user.authenticate('cde34rfv') false > user.authenticate('zaq12wsx') #<User id: 1, name: "hikotaro", email: "hikotaro@example.co.jp", password_digest: "$2a$12$bWRsPGrzgCac0g0IYgjW8OXshnDwaEmoIcHeJ2AP3ot...", ....