Archive for: 𔃱月 2017’

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

RustでHello World

2017年1月27日 Posted by PURGE

最近、Rustという言語を知った。
ちょっと興味を持ったので、ハローワールドしてみた。

環境
OS:Windows7

下記のページでインストーラを取得してインストール。
https://www.rust-lang.org/en-US/other-installers.html

チュートリアル通りではあるが、下記のファイルを作成しコンパイル。

main.rs

fn main(){
	println!("こんにちは. たぢさん。");
}
c:\work\> rustc main.rs

すると、Window環境なので、main.exe が生成される。

c:\work\> main.exe
こんにちは たぢさん。

楽しそうだ。暇があったら、勉強してみようかと思う。

cx_Oracle でOracle接続

2017年1月26日 Posted by PURGE

開発環境が、Windowsで、データベースの文字コードはSJIS。

cx_Oracleを使用して、Oracle接続をしてみたが、どうしても出力結果が文字化けしてしまう。
そこで、下記にて環境変数を指定してみた。

import os
os.environ["NLS_LANG"] = "JAPANESE_JAPAN.JA16SJISTILDE"
import cx_Oracle
import os

os.environ["NLS_LANG"] = "JAPANESE_JAPAN.JA16SJISTILDE"
db_user = 'user'
db_pass = 'xxxx'
db_host = '(DESCRIPTION=(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)) (CONNECT_DATA =(SERVER = DEDICATED)(SID = HOGE)))'
con = cx_Oracle.connect(db_user, db_pass, db_host)

cur = con.cursor()
sql = 'select member_id, member_name from members order by 1'
cur.execute(sql)
rows = cur.fetchall()
for row in rows:
    member_id = row[0]
    member_name = row[1]
    print( member_name)

うまくいった。

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"