20190203のRailsに関する記事は4件です。

Windowsでrubyをつかう。WEBrickが立ち上がるまで

WindosでRubyの開発を行いたいと思ったことがきっかけ
Ruby導入からRailsプロジェクトの作成、WEBrickが起動するまでのまとめ
Qiitaとか投稿したこがないけど、やってみようかな
アウトプットすることを覚える
見苦しいところが多々ありますが、ご指摘していただけるとありがだいです。m@@m

開発環境

  • Windows7(x64)
  • Ruby2.5.3p105 (2018-10-18 revision 65156)
  • Rails5.2.2

rubyインストール

Windows用のRubyがここで公開されてる。development kit がついている以下のものをダウンロードする

  • Ruby+Devkit 2.5.3-1 (x64)

インストーラを実行すればrubyが導入される
今回はC直下にインストールv@@v

Railsのインストール

コマンドプロンプトで以下を実行する

gem install rails

SQLite3の実行ファイルをダウンロード

ここから以下のものをダウンロードする

  • Source Code

    • sqlite-amalgamation-3260000.zip・・・C直下に展開
  • recompiled Binaries for Windows

    • sqlite-dll-win32-x86-3260000.zip・・・C:\Ruby25-x64\binにsqlite3.dllを投入
    • sqlite-tools-win32-x86-3260000.zip・・・C:\Ruby25-x64\binにsqlite3.exeを投入

SQLite3のライブラリをインストール

SQLite3のソースコードと実行ファイルのディレクトリを指定しコマンドプロンプトで以下を実行

gem install sqlite3 --platform=ruby -- --with-sqlite3-include=C:\sqlite-amalgamation-3260000 --with-sqlite3-lib=C:\Ruby25-x64\bin

※SQLite3のインストールでハマりました、以下のページを参照して脱出
 WindowsでRailsTutorialするときに気をつけること

Railsプロジェクトを作成

コマンドプロンプトでRailsのプロジェクトを作成したいフォルダを指定(今回はd:\app\rails)し以下を実行

rails new プロジェクト名

d:\app\railsにプロジェクト名のフォルダが作成されている
これだけでrailsのプロジェクトができちゃうのね

WEBrickを起動する

d:\app\rails\プロジェクト名 を指定しコマンドプロンプトで以下を実行

rails server WEBrick

http://localhost:3000/にアクセス
WEBrickが正常に起動していれば、地球の上に立っている人々が表示される

コマンドでWEBrickを指定せず、サーバを起動させるとPumaが起動した
バージョンによってデフォルトで起動するサーバが違うのか...?

参考

Ruby on Rails入門
ここで紹介されている内容を実行しました

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

brew doctorでWarningが出たので解決方法まとめました。(for Mac)

環境

Macbook pro(2017年モデル)
macOS Mojave

状況

Homebrewを使って、rbenvをインストールしようと試みたところエラーが発生し、

terminal
$ brew doctor

で確認したところ、以下のようなWarningが出現したので、その解決方法を備忘録として残しておきます。

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
  /usr/local/lib/libgvplugin_core.6.dylib
  /usr/local/lib/libgvplugin_dot_layout.6.dylib
  /usr/local/lib/libgvplugin_gd.6.dylib
  /usr/local/lib/libgvplugin_neato_layout.6.dylib
  /usr/local/lib/libgvplugin_quartz.6.dylib

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
  /usr/local/include/arith.h
  /usr/local/include/binary_log_types.h
  /usr/local/include/cdt.h
  /usr/local/include/cgraph.h
  /usr/local/include/color.h
  /usr/local/include/decode.h
  /usr/local/include/demux.h
  /usr/local/include/encode.h
  /usr/local/include/errmsg.h
  /usr/local/include/fcfreetype.h
  /usr/local/include/fcprivate.h
  /usr/local/include/fontconfig.h
  /usr/local/include/freetype/config/ftconfig.h
  /usr/local/include/freetype/config/ftheader.h
  /usr/local/include/freetype/config/ftmodule.h
  /usr/local/include/freetype/config/ftoption.h
  /usr/local/include/freetype/config/ftstdlib.h
  /usr/local/include/freetype/freetype.h
  /usr/local/include/freetype/ftadvanc.h
  /usr/local/include/freetype/ftbbox.h
  /usr/local/include/freetype/ftbdf.h
  /usr/local/include/freetype/ftbitmap.h
  /usr/local/include/freetype/ftbzip2.h
  /usr/local/include/freetype/ftcache.h
  /usr/local/include/freetype/ftchapters.h
  /usr/local/include/freetype/ftcid.h
  /usr/local/include/freetype/ftdriver.h
  /usr/local/include/freetype/fterrdef.h
  /usr/local/include/freetype/fterrors.h
  /usr/local/include/freetype/ftfntfmt.h
  /usr/local/include/freetype/ftgasp.h
  /usr/local/include/freetype/ftglyph.h
  /usr/local/include/freetype/ftgxval.h
  /usr/local/include/freetype/ftgzip.h
  /usr/local/include/freetype/ftimage.h
  /usr/local/include/freetype/ftincrem.h
  /usr/local/include/freetype/ftlcdfil.h
  /usr/local/include/freetype/ftlist.h
  /usr/local/include/freetype/ftlzw.h
  /usr/local/include/freetype/ftmac.h
  /usr/local/include/freetype/ftmm.h
  /usr/local/include/freetype/ftmodapi.h
  /usr/local/include/freetype/ftmoderr.h
  /usr/local/include/freetype/ftotval.h
  /usr/local/include/freetype/ftoutln.h
  /usr/local/include/freetype/ftparams.h
  /usr/local/include/freetype/ftpfr.h
  /usr/local/include/freetype/ftrender.h
  /usr/local/include/freetype/ftsizes.h
  /usr/local/include/freetype/ftsnames.h
  /usr/local/include/freetype/ftstroke.h
  /usr/local/include/freetype/ftsynth.h
  /usr/local/include/freetype/ftsystem.h
  /usr/local/include/freetype/fttrigon.h
  /usr/local/include/freetype/fttypes.h
  /usr/local/include/freetype/ftwinfnt.h
  /usr/local/include/freetype/t1tables.h
  /usr/local/include/freetype/ttnameid.h
  /usr/local/include/freetype/tttables.h
  /usr/local/include/freetype/tttags.h
  /usr/local/include/ft2build.h
  /usr/local/include/geom.h
  /usr/local/include/graphviz_version.h
  /usr/local/include/gvc.h
  /usr/local/include/gvcext.h
  /usr/local/include/gvcjob.h
  /usr/local/include/gvcommon.h
  /usr/local/include/gvconfig.h
  /usr/local/include/gvplugin.h
  /usr/local/include/gvplugin_device.h
  /usr/local/include/gvplugin_layout.h
  /usr/local/include/gvplugin_loadimage.h
  /usr/local/include/gvplugin_render.h
  /usr/local/include/gvplugin_textlayout.h
  /usr/local/include/gvpr.h
  /usr/local/include/lt_dlloader.h
  /usr/local/include/lt_error.h
  /usr/local/include/lt_system.h
  /usr/local/include/mux.h
  /usr/local/include/mux_types.h
  /usr/local/include/my_command.h
  /usr/local/include/my_list.h
  /usr/local/include/mysql.h
  /usr/local/include/mysql/client_plugin.h
  /usr/local/include/mysql/plugin_auth_common.h
  /usr/local/include/mysql/udf_registration_types.h
  /usr/local/include/mysql_com.h
  /usr/local/include/mysql_time.h
  /usr/local/include/mysql_version.h
  /usr/local/include/mysqld_error.h
  /usr/local/include/mysqlx_ername.h
  /usr/local/include/mysqlx_error.h
  /usr/local/include/mysqlx_version.h
  /usr/local/include/pack.h
  /usr/local/include/pathgeom.h
  /usr/local/include/pathplan.h
  /usr/local/include/png.h
  /usr/local/include/pngconf.h
  /usr/local/include/textspan.h
  /usr/local/include/types.h
  /usr/local/include/usershape.h
  /usr/local/include/xdot.h

解決法

まずは上のWarningから。(下のWarningも解決方法同じでした!!)

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
  /usr/local/lib/libgvplugin_core.6.dylib
  /usr/local/lib/libgvplugin_dot_layout.6.dylib
  /usr/local/lib/libgvplugin_gd.6.dylib
  /usr/local/lib/libgvplugin_neato_layout.6.dylib
  /usr/local/lib/libgvplugin_quartz.6.dylib

①ターミナルに以下を入力します。(ファイルの作成)

terminal
$ vim ~/.del_files.sh

が並んだページが出現するので、1行目に#!/bin/bashを貼り付け、2行目以降にUnexpected dylibs:に記載されているファイルをコピペします。(以下、参考)

terminal
#!/bin/bash
  /usr/local/lib/libgvplugin_core.6.dylib
  /usr/local/lib/libgvplugin_dot_layout.6.dylib
  /usr/local/lib/libgvplugin_gd.6.dylib
  /usr/local/lib/libgvplugin_neato_layout.6.dylib
  /usr/local/lib/libgvplugin_quartz.6.dylib

③カーソルを一番上のファイル(2行目)の頭におき、ctrl + vで矩形選択のビジュアルモード(VISUAL BLOCK)に移行。カーソルで一番下のファイルの頭まで選択し、:を押します。

すると、下のラインに既に :'<,'>が入力された状態でコマンドラインが表示されるので、続けて s/^/rm -f /と入力します。(rm は、ファイルやディレクトリを削除してあげるコマンドで、 -f は、「削除しますか?」的なメッセージを都度出さないようにするオプションです。)

Enterを押して、全ての行(一番上以外)の先頭にs/^/rm -f /が追加されているのを確認したら、もう一度:を押します。一番下の行に入力欄が出てくるので、wq!と入力し、Enterを押します。

⑤通常の入力モードに戻るので、そこで

terminal
$ bash ~/.del_files.sh

を実行。これでファイルが削除されます。

⑥以上が完了したら、

terminal
$ brew doctor

を実行し、エラー文が消えていたら、成功です。

追記

⑦もし、以下のようなWarningが出現した場合

Warning: Broken symlinks were found. Remove them with `brew cleanup`:
  /usr/local/lib/libgvplugin_core.dylib
  /usr/local/lib/libgvplugin_dot_layout.dylib
  /usr/local/lib/libgvplugin_gd.dylib
  /usr/local/lib/libgvplugin_neato_layout.dylib
  /usr/local/lib/libgvplugin_quartz.dylib

エラー文に従って、

terminal
$ brew cleanup

してあげましょう。


こちらのWarningも上記と同じ方法で解決できます!!

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
  /usr/local/include/arith.h
  /usr/local/include/binary_log_types.h
  /usr/local/include/cdt.h
  /usr/local/include/cgraph.h
  /usr/local/include/color.h
  /usr/local/include/decode.h
  /usr/local/include/demux.h
  /usr/local/include/encode.h
  /usr/local/include/errmsg.h
  /usr/local/include/fcfreetype.h
  /usr/local/include/fcprivate.h
  /usr/local/include/fontconfig.h
  /usr/local/include/freetype/config/ftconfig.h
  /usr/local/include/freetype/config/ftheader.h
  /usr/local/include/freetype/config/ftmodule.h
  /usr/local/include/freetype/config/ftoption.h
  /usr/local/include/freetype/config/ftstdlib.h
  /usr/local/include/freetype/freetype.h
  /usr/local/include/freetype/ftadvanc.h
  /usr/local/include/freetype/ftbbox.h
  /usr/local/include/freetype/ftbdf.h
  /usr/local/include/freetype/ftbitmap.h
  /usr/local/include/freetype/ftbzip2.h
  /usr/local/include/freetype/ftcache.h
  /usr/local/include/freetype/ftchapters.h
  /usr/local/include/freetype/ftcid.h
  /usr/local/include/freetype/ftdriver.h
  /usr/local/include/freetype/fterrdef.h
  /usr/local/include/freetype/fterrors.h
  /usr/local/include/freetype/ftfntfmt.h
  /usr/local/include/freetype/ftgasp.h
  /usr/local/include/freetype/ftglyph.h
  /usr/local/include/freetype/ftgxval.h
  /usr/local/include/freetype/ftgzip.h
  /usr/local/include/freetype/ftimage.h
  /usr/local/include/freetype/ftincrem.h
  /usr/local/include/freetype/ftlcdfil.h
  /usr/local/include/freetype/ftlist.h
  /usr/local/include/freetype/ftlzw.h
  /usr/local/include/freetype/ftmac.h
  /usr/local/include/freetype/ftmm.h
  /usr/local/include/freetype/ftmodapi.h
  /usr/local/include/freetype/ftmoderr.h
  /usr/local/include/freetype/ftotval.h
  /usr/local/include/freetype/ftoutln.h
  /usr/local/include/freetype/ftparams.h
  /usr/local/include/freetype/ftpfr.h
  /usr/local/include/freetype/ftrender.h
  /usr/local/include/freetype/ftsizes.h
  /usr/local/include/freetype/ftsnames.h
  /usr/local/include/freetype/ftstroke.h
  /usr/local/include/freetype/ftsynth.h
  /usr/local/include/freetype/ftsystem.h
  /usr/local/include/freetype/fttrigon.h
  /usr/local/include/freetype/fttypes.h
  /usr/local/include/freetype/ftwinfnt.h
  /usr/local/include/freetype/t1tables.h
  /usr/local/include/freetype/ttnameid.h
  /usr/local/include/freetype/tttables.h
  /usr/local/include/freetype/tttags.h
  /usr/local/include/ft2build.h
  /usr/local/include/geom.h
  /usr/local/include/graphviz_version.h
  /usr/local/include/gvc.h
  /usr/local/include/gvcext.h
  /usr/local/include/gvcjob.h
  /usr/local/include/gvcommon.h
  /usr/local/include/gvconfig.h
  /usr/local/include/gvplugin.h
  /usr/local/include/gvplugin_device.h
  /usr/local/include/gvplugin_layout.h
  /usr/local/include/gvplugin_loadimage.h
  /usr/local/include/gvplugin_render.h
  /usr/local/include/gvplugin_textlayout.h
  /usr/local/include/gvpr.h
  /usr/local/include/lt_dlloader.h
  /usr/local/include/lt_error.h
  /usr/local/include/lt_system.h
  /usr/local/include/mux.h
  /usr/local/include/mux_types.h
  /usr/local/include/my_command.h
  /usr/local/include/my_list.h
  /usr/local/include/mysql.h
  /usr/local/include/mysql/client_plugin.h
  /usr/local/include/mysql/plugin_auth_common.h
  /usr/local/include/mysql/udf_registration_types.h
  /usr/local/include/mysql_com.h
  /usr/local/include/mysql_time.h
  /usr/local/include/mysql_version.h
  /usr/local/include/mysqld_error.h
  /usr/local/include/mysqlx_ername.h
  /usr/local/include/mysqlx_error.h
  /usr/local/include/mysqlx_version.h
  /usr/local/include/pack.h
  /usr/local/include/pathgeom.h
  /usr/local/include/pathplan.h
  /usr/local/include/png.h
  /usr/local/include/pngconf.h
  /usr/local/include/textspan.h
  /usr/local/include/types.h
  /usr/local/include/usershape.h
  /usr/local/include/xdot.h

参考URL

brew doctorでのWarningを解決してみた(for Mac)
HowTo: Save File in Vi / Vim Text Editor
server - E45 readonly option is set (add ! to override) - Ask Ubuntu

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

RailsでのER図作成【gem】

RailsでER図を作成する便利なライブラリのインストールと使い方

必要な各ライブラリをインストール

  • rails-erd 【gem】
  • graphviz

必要なのはこの2つ
※ homebrewが必要なので事前にインストール

rails-erdのインストール

Gemfileに追記

Gemfile
group :development do
...
  gem "rails-erd" # 追加
...
end

できたら下記コマンドでインストール

$ bundle install

graphvizのインストール

$ brew install graphviz

これで必要なライブラリのインストールは終わり

ER図出力

下記コマンドを使用するとカレントディレクトリにerd.dotファイルが生成される

$ bundle exec erd --inheritance --direct --attributes=foreign_keys,content --filetype=dot

dotファイルを閲覧できる形式に変換

  • pngに変換
$ dot -Tpng erd.dot -o erd.png
  • pdfに変換
$ dot -Tpdf erd.dot -o erd.pdf
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

ActiveHash (ActiveYaml) を i18n 対応する

ApplicationRecord と同じように i18n できなかったのでメモしておく。

config/application.rb
# ...
Bundler.require(*Rails.groups)

class ActiveHash::Base
  extend ActiveModel::Translation
end

module YourAppName
  class Application < Rails::Application
# ...
config/locales/ja.yml
ja:
  activemodel:
    models:
      model_name: モデル名

これをやらずとも、 https://github.com/zilkey/active_hash/pull/170 がマージされれば、ロケールを設定するだけでよくなる。

効用

class MyModel < ActiveYaml::Base
end

# これで、以下のメソッドたちの返り値をロケールから変更できる
MyModel.model_name.human
MyModel.human_attribute_name(:column_name)
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む