Category: ‘Rails’

Rails5 でdeviseとomniauthを使用する

2017年1月30日 Posted by PURGE

Gemfileに必要なモジュールを記述する。

■Gemfile

gem 'devise'
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'

Deviseのインストール。

$ rails g devise:install
Running via Spring preloader in process 3689
Expected string default value for '--jbuilder'; got true (boolean)
      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml

deviseの設定ファイルが生成される。

■config/initializer/devise.rb

Devise.setup do |config|
 ・・・
end

■db/migrate/YYYYMMDDhhmmss_add_devise_to_users.rb

class AddDeviseToUsers < ActiveRecord::Migration[5.0]
 ・・・
end

Userモデルを作成する。

$ rails g devise User
Running via Spring preloader in process 3757
Expected string default value for '--jbuilder'; got true (boolean)
      invoke  active_record
   identical    db/migrate/20170130065955_add_devise_to_users.rb
      insert    app/models/user.rb
       route  devise_for :users

DBのマイグレーションを実行。

$ rails db:migrate
== 20170130065955 AddDeviseToUsers: migrating =================================
-- change_table(:users)
   -> 0.3352s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0278s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0336s
== 20170130065955 AddDeviseToUsers: migrated (0.3970s) ========================

■ハマった点。

下記のエラーに悩まされた。
上記を順番通りに実行すれば、問題ないはず。

dynamic_matchers.rb:21:in `method_missing': undefined method `devise' for User

gem install rmagick でインストール失敗

2017年1月21日 Posted by PURGE

結構ハマったので覚え書き。

数々の解決方法が、Webには転がっていたのだが、今回のケースはどれにも当はまらなかったので、独自の解決方法を記す。

環境
Mac : 10.12.2
Ruby: 2.2.2
ImageMagick : 6.9.1

最初、brew によって、ImageMagic をインストールして、gem install rmagick を実行していたが、下記のエラーが頻発する。

$ gem install rmagick -v '2.16.0'
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
        ERROR: Failed to build gem native extension.

    /Users/xxxx/.rbenv/versions/2.2.2/bin/ruby -r ./siteconf20170121-33287-t9qj7o.rb extconf.rb
checking for clang... yes
checking for Magick-config... no
checking for pkg-config... yes
checking for outdated ImageMagick version (<= 6.4.9)... no
checking for presence of MagickWand API (ImageMagick version >= 6.9.0)... no
checking for Ruby version >= 1.8.5... yes
checking for stdint.h... yes
checking for sys/types.h... yes
checking for wand/MagickWand.h... no

Can't install RMagick 2.16.0. Can't find MagickWand.h.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/Users/xxxx/.rbenv/versions/2.2.2/bin/$(RUBY_BASE_NAME)

extconf failed, exit code 1

そこで、このサイト(http://cactuslab.com/imagemagick/)から、ダウンロードして、インストールした。

デフォルトでは、下記のディレクトリにインストールされる。

/opt/ImageMagick

参考までに、brew でインストールした場合は、下記にインストールされる。

/usr/local/Celler/imagemagick

重複しない為に、brewでインストールしたものは、アンインストールした。

$ brew uninstall ImageMagick

念の為、ImageMagickが、想定通りのものか確認するために、下記コマンドにて確認。

$ convert --version
Version: ImageMagick 6.9.1-0 Q16 x86_64 2015-03-22 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: DPC OpenCL
Delegates (built-in): bzlib fftw jng jpeg lcms lzma png tiff webp xml zlib

$ which convert
/opt/ImageMagick/bin/convert

■環境変数にpkgconfigを追記する。

$ vi .bash_profile

# Setting PATH for ImageMagick
export PKG_CONFIG_PATH=/opt/ImageMagick/lib/pkgconfig

$ source .bash_profile

■ rmagickのインストール

$ gem install rmagick
Building native extensions.  This could take a while...
Successfully installed rmagick-2.16.0
Parsing documentation for rmagick-2.16.0
Installing ri documentation for rmagick-2.16.0
Done installing documentation for rmagick after 10 seconds
1 gem installed

■まとめ
ポイントとしては、
 1. brew で、ImageMagic をインストールしない。
 2. PKG版のImageMagicをインストールして、pkgconfig ディレクトリを環境変数に追記する。

なかなか、しんどかった。

Bluemix cf push エラー

2017年1月2日 Posted by PURGE

今まで問題なくBluemixにアップロード(cf push)できていたのに突然エラーでアップロードできなくなった。
結論から言うと、最新の CFコマンドラインインターフェースをインストールすること。

cf logs AppName --recent
fatal error: unexpected signal during runtime execution

GoのErrorだったので、ハマっていたが何てこと無い。

Bluemix でRailsのDBマイグレーション

2017年1月2日 Posted by PURGE

ローカルの開発環境では、気にせずにDBマイグレーションを行える。
下記は、Blumix上でのマイグレーションの方法の覚え書き。

cf push AppName -c "bundle exec rake db:migrate"
cf push AppName -c "null"

bundle install が nokogiri のライブラリ依存で失敗した場合

2016年12月6日 Posted by PURGE

bundle install が、nokogiri のライブラリ依存で失敗した場合。

Installing nokogiri 1.6.8.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/xxxxx/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.6.8.1/ext/nokogiri
/Users/xxxxx/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20161206-45292-57wgxr.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Using mini_portile version 2.1.0
checking for iconv.h... yes
checking for gzdopen() in -lz... yes
checking for iconv... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.4.

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************

brewで、libxml2 をインストールして、bundleのオプションで、libxml2 のライブラリの場所を指定する。

$ brew install libxml2
$ bundle config build.nokogiri --use-system-libraries --with-xml2-include=/usr/local/opt/libxml2/include/libxml2/
$ bundle install

Rails5アプリをBluemix でDB migrate してエラー

2016年8月19日 Posted by PURGE

ローカルの開発環境では、rake db:migrate はコマンド打てば簡単だが、Bluemix等のクラウド環境でのコマンドの実行方法が不明であった。どうやら、push するタイミングで下記のように -c オプションで可能なようだ。

$ cf push <appname> -c "bundle exec rake db:migrate"

DBを見ると、うまくマイグレーションされているようだが、その後アプリが、下記のエラーで起動しない。

ERR Instance (index 0) failed to start accepting connections

調べてみると、先ほど指定したオプションが邪魔しているようで、nullでクリアする必要があるようだ。

$ cf push <appname> -c "null"

その後、cf push で、普通通りにアプリがデプロイされて起動できた。

Capstrano3とRuby2.2 on Rails4.2 の設定

2015年10月18日 Posted by PURGE

Capstrano3を使用しようと試しているが、かなりハマっている。
下記は覚え書き。

rbenvでrubyのバージョンが設定されていないエラー

ERROR rbenv: rbenv_ruby is not set

rbenvのパスが検出されないエラー

ERROR rbenv: 2.2.2 is not installed or not found in ~/.rbenv/versions/2.2.2

deploy.rb

## デプロイサーバのパスを記述
set :rbenv_path, &#039;/var/rbenv&#039;
set :rbenv_ruby, &#039;2.2.2&#039;
set :application, &#039;Sample&#039;
set :repo_url, &#039;ssh://hoge@whoocus.com/path/to/git/repo/sample.git&#039;

サーバ上でbundleが見つからないエラー

bundle stderr: rbenv: bundle: command not found

サーバ上でインストール。

gem install bundler

JavaScript runtimeが見つからないとのエラー。

JS::RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.

Gemfile

gem &#039;execjs&#039;
gem &#039;therubyracer&#039;, platforms: :ruby

Capistrano3.x の覚え書き。

2015年9月22日 Posted by PURGE

以前といっても既に、3年程前に、Capstranoを設定していたが、既にCapstrano3.xの時代。
久しぶりに設定する機会があったので覚え書き。

Railsの設定
Gemfile にデフォルトでコメントアウトされている下記の記述を有効にして、bundle update 。

# Use Capistrano for deployment
gem 'capistrano-rails', group: :development
$ bundle update
$ capify .
--------------------------------------------------------------------------------
Capistrano 3.x is incompatible with Capistrano 2.x. 

This command has become `cap install` in Capistrano 3.x

For more information see http://www.capistranorb.com/
--------------------------------------------------------------------------------

capify . にて設定ファイルを作成しようとしたが、怒られたので、cap install を実行する。

$ cap install
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
create Capfile
Capified

すると、Capfile と、config/deploy.rb、config/deploy/staging.rb、config/deploy/production.rb が作成される。

Gitのサーバ設定

Gitのインストール。

$ yum install git

Git共有リポジトリの作成。

$ mkdir /var/lib/git/public_git/sample.git
$ cd /var/lib/git/public_git/sample.git
$ git init --bare --share 

Gitのクライアント設定

$ git init
$ git add .
$ git commit -m "First commit"
$ git remote add sample ssh://User名@Host名:SSHポート番号/var/lib/git/public_git/sample.git
$ git push sample

Redmine2.1.2からRedmine3.1.0へのDB移行時のエラー

2015年8月24日 Posted by PURGE

Mysqlのダンプファイルをインポートしようとしたが、Redmineがサーバエラーを起こしていたので覚え書き。

データダンプファイルを新規のDBに移行し、新たにマイグレーション。

$ mysql redmine_db < Dmpfile.sql
$ rake db:migrate RAILS_ENV=production

エラーログを見るとマイグレーションで失敗していたようなので、下記のテーブル群が件数0件であることを確認してDROP。

drop table custom_fields_roles;
drop table queries_roles;
drop table email_addresses;
drop table roles_managed_roles;

その後も下記エラーとなったが、Redmineのサーバを再起動したら直った。

Mysql2::Error: Unknown column 'users.mail'

gem install mysql2 インストールエラー

2015年8月14日 Posted by PURGE

対象環境

  • CentOS 6.7
  • Ruby2.1
  • Rails4.1
  • MySQL Community Server 5.6.26

概要

Redmineをインストールするにあたって、MySQLをインストールし、Railsで使用するライブラリmysql2のインストールでエラーとなる。

$ gem install mysql2 -v '0.3.19'
Fetching: mysql2-0.3.19.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

    /usr/local/ruby2.2/bin/ruby -r ./siteconf20150814-32250-1l3iwxf.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/bin/mysql_config
-----
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing. Try 'apt-get install libmysqlclient-dev' or
'yum install mysql-devel', check your installation of mysql and try again.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/local/ruby2.2/bin/$(RUBY_BASE_NAME)
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysql-config
        --without-mysql-config

extconf failed, exit code 1

Windows環境でハマった時の対応方法は、この記事であるが、今回の環境はまた異なるし、対応方法が異なるようだ。

そこで下記`mysql-devel`をインストール。

$ yum install mysql-devel
読み込んだプラグイン:fastestmirror, security
インストール処理の設定をしています
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: centos.usonyx.net
パッケージ mysql-devel は mysql-community-devel によって不要になりました。代わりに mysql-community-devel-5.6.26-2.el6.x86_64 のインストールを試みています。
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> Package mysql-community-devel.x86_64 0:5.6.26-2.el6 will be インストール
--> 依存性解決を終了しました。

依存性を解決しました

====================================================================================================
 パッケージ                    アーキテクチャ バージョン            リポジトリー               容量
====================================================================================================
インストールしています:
 mysql-community-devel         x86_64         5.6.26-2.el6          mysql56-community         3.2 M

トランザクションの要約
====================================================================================================
インストール         1 パッケージ

総ダウンロード容量: 3.2 M
インストール済み容量: 20 M
これでいいですか? [y/N]y
パッケージをダウンロードしています:
mysql-community-devel-5.6.26-2.el6.x86_64.rpm                                | 3.2 MB     00:01
rpm_check_debug を実行しています
トランザクションのテストを実行しています
トランザクションのテストを成功しました
トランザクションを実行しています
  インストールしています  : mysql-community-devel-5.6.26-2.el6.x86_64                           1/1
  Verifying               : mysql-community-devel-5.6.26-2.el6.x86_64                           1/1

インストール:
  mysql-community-devel.x86_64 0:5.6.26-2.el6

完了しました!

再度、試したら上手く行った。

$ gem install mysql2
Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.19
Parsing documentation for mysql2-0.3.19
Installing ri documentation for mysql2-0.3.19
Done installing documentation for mysql2 after 1 seconds
1 gem installed