Category: ‘Ruby’

Rubyのループ文

2014年9月9日 Posted by PURGE

なかなか覚えられないので覚え書き。
便利だが文法で混乱する。

str = ["あ","い","う","え","お"]

#for文
for s1 in str do
  puts s1
end

#each do
str.each do |s2|
  puts s2
end

#each{}
str.each{|s3|
  puts s3
}

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

2013年8月28日 Posted by PURGE

バージョンアップのたびに、何度もハマる。
今回は、WindowsXP + ruby2.0 + mysql5.5

おなじみ下記のエラー。

checking for main() in -llibmysql... no
*** 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=C:/usr/local/ruby200/bin/ruby
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include=${mysql-dir}/include
        --with-mysql-lib=${mysql-dir}/
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include=${mysql-dir}/include
        --with-mysql-lib=${mysql-dir}/lib
        --with-libmysqllib
        --without-libmysqllib
C:\>gem install mysql2 -- '--with-mysql-include="C:\Program Files\MySQL\MySQL Server 5.5\include" --with-mysql-lib="C:\Program Files\MySQL\MySQL Server 5.5\lib"'
Temporarily enhancing PATH to include DevKit...
Building native extensions with: '--with-mysql-include="C:\Program Files\MySQL\MySQL Server 5.5\incl
ude" --with-mysql-lib="C:\Program Files\MySQL\MySQL Server 5.5\lib"'
This could take a while...
Successfully installed mysql2-0.3.13
Parsing documentation for mysql2-0.3.13
unable to convert "\x90" from ASCII-8BIT to UTF-8 for lib/mysql2/mysql2.so, skipping
Installing ri documentation for mysql2-0.3.13
1 gem installed

毎回、gem install の引数の渡し方が良くない。シングルクウォートで囲う。

Heroku エラー

2013年8月11日 Posted by PURGE

ローカルの開発環境では動作しているアプリが、herokuにデプロイすると動作せずに下記エラー。

$heroku logs -t

at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=xxxx.herokuapp.com fwd="111.111.111.111" dyno= connect= service= status=503 bytes=

一度、アプリ自体を削除したが、同様のエラー。

結局、ローカルの環境から作成しなおした。
かなり面倒臭かった。原因不明。

rails rake db:migrate でカラム名変更

2013年8月7日 Posted by PURGE

まずは、マイグレーションファイルの自動生成。

rails generate migration RenameColumnToTargetTable

すると、下記のマイグレーションファイルが生成される。
ちなみにファイル名は環境によって異なる。

2013080112000000_rename_column_to_target_table.rb

class RenameColumnToTargetTable < ActiveRecord::Migration
  def change
    rename_column :target_table, : old_column, :new_column
  end
end

マイグレートの実行。

rake db:migrate

rake db:migrateを使用しての、運用中のDBへの更新は初めてで怖かったのであるが、難なく成功。

当然、migrate実行する前に、必ずバックアップ取っておいてね。

Herokuで使用しているClearDBへの接続

2013年6月1日 Posted by PURGE

個人的には MySQLが好きなのであるが、Herokuでは、デフォルトのDBが、Heroku Postgres になっている。
そこで、Heroku Add-onである ClearDBというMySQLアドオンを設定して利用してみる。

まずは、Herokuサイトにてログインして、ClearDBをアドオンしてみる。Igniteであれば 5MB無料で利用できる。

そして、Heroku Toolbeltコマンドラインからログインし、アドオンされた ClearDB の設定情報を確認してみる。

$ heroku config | grep CLEARDB_DATABASE_URL
CLEARDB_DATABASE_URL: mysql://userid:password@hostname.cleardb.com/heroku_12345?reconnect=true

上記の値を、DATABASE_URL (デフォルトではPostgresが設定されている)で上書き設定する。
注意点としては、使用するMySQLによって、 mysql2:// に変更すること。

$ heroku config:add DATABASE_URL='mysql2://userid:password@hostname.cleardb.com/heroku_12345?reconnect=true

問題なく設定できていれば、git pushしたアプリのmigrationが成功するはずである。

$ heroku run rake db:migrate

尚、上記の設定情報からもわかるように、MySQLツールにて設定すれば直接DBが参照可能となる。

Rails server でエラー

2013年3月21日 Posted by PURGE

新しい環境を作るのは中々一筋縄ではいかないもので。
mysqlのlibが見つからないとのことで、rails server が起動しない。

$ rails server
/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
  Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
  Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

そこで、下記のシンボリックリンクを作成した。
ちなみに、環境はMacなので、/usr/local/mysql/lib/libmysqlclient.18.dylib にライブラリ関係がインストールされているようだ。

$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
$ rails server
=> Booting WEBrick
=> Rails 3.2.12 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-03-21 09:55:44] INFO  WEBrick 1.3.1
[2013-03-21 09:55:44] INFO  ruby 1.8.7 (2012-02-08) [universal-darwin12.0]
[2013-03-21 09:55:44] INFO  WEBrick::HTTPServer#start: pid=2178 port=3000

成功。

MacにRubyをインストールする

2013年3月17日 Posted by PURGE

新しくMacBookAirを購入した為、開発環境を構築することとなった。その覚え書き。

まずは、rvm をインストールするのであるが、以前のrvmインストール方法とURLが変わったようである。

以前は、下記のようであったが、

bash << (curl -s https://rvm.beginrescueend.com/install/rvm)

現在は、下記の感じ。

curl -L https://get.rvm.io | bash -s stable

rvmをインストールすると、$HOME/.rvm 以下にインストールされるので、そこにPATHを通す為に、.bash_profile に記述する。

echo '[[ -s "$HOME/.rvm/scripts/rvm"]] && . "$HOME/.rvm/scripts/rvm" #Load RVM function' >> ~/.bash_profile

そして、rvmでruby1.9.3 をインストールする。

rvm install 1.9.3

Fetching yaml-0.1.4.tar.gz to /Users/tajihiro/.rvm/archives
Extracting yaml to /Users/xxx/.rvm/src/yaml-0.1.4
Error running '__rvm_package_extract /Users/xxx/.rvm/archives/yaml-0.1.4.tar.gz /Users/xxx/.rvm/src', please read /Users/xxx/.rvm/log/ruby-1.9.3-p392/yaml/extract.log
There has been an error while trying to extract the source. Halting the installation.
Installing Ruby from source to: /Users/xxx/.rvm/rubies/ruby-1.9.3-p392, this may take a while depending on your cpu(s)...
ruby-1.9.3-p392 - #downloading ruby-1.9.3-p392, this may take a while depending on your connection...
######################################################################## 100.0%
ruby-1.9.3-p392 - #extracting ruby-1.9.3-p392 to /Users/tajihiro/.rvm/src/ruby-1.9.3-p392
ruby-1.9.3-p392 - #extracted to /Users/xxx/.rvm/src/ruby-1.9.3-p392
ruby-1.9.3-p392 - #configuring
ruby-1.9.3-p392 - #compiling
Error running 'make', please read /Users/xxx/.rvm/log/ruby-1.9.3-p392/make.log
There has been an error while running make. Halting the installation.

しかし、エラー。

そこで、諦めて brew にて、ruby をインストールしてみる。rvmは使用しないことにした。

Railsセキュリティアップデート

2013年1月16日 Posted by PURGE

2013年1月8日にRailsのセキュリティアップデートがリリースされた。
使用しているRailsのバージョンは、3.2.6だったので、アプリのGemfileを下記のように書き換えてみた。

gem 'rails', '3.2.11'
#gem 'rails', '3.2.6'

アプリケーションのトップディレクトリでアップデートしてみた。

$ bundle update
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Installing rake (10.0.3)
Using i18n (0.6.1)
Installing multi_json (1.5.0)
Installing activesupport (3.2.11)
Using builder (3.0.4)
Installing activemodel (3.2.11)
Using erubis (2.7.0)
Using journey (1.0.4)
Installing rack (1.4.4)
Using rack-cache (1.2)
Using rack-test (0.6.2)
Using hike (1.2.1)
Using tilt (1.3.3)
Installing sprockets (2.2.2)
Installing actionpack (3.2.11)
Using mime-types (1.19)
Using polyglot (0.3.3)
Installing treetop (1.4.12)
Using mail (2.4.4)
Installing actionmailer (3.2.11)
Using arel (3.0.2)
Installing tzinfo (0.3.35)
Installing activerecord (3.2.11)
Installing activeresource (3.2.11)
Using bundler (1.1.3)
Using coffee-script-source (1.4.0)
Using execjs (1.4.0)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Installing json (1.7.6) with native extensions
Using rdoc (3.12)
Using thor (0.16.0)
Installing railties (3.2.11)
Using coffee-rails (3.2.2)
Installing jquery-rails (2.2.0)
Using mysql2 (0.3.10)
Installing rails (3.2.11)
Installing sass (3.2.5)
Installing sass-rails (3.2.6)
Using uglifier (1.3.0)
Your bundle is updated! Use `bundle show [gemname]` to see where a bundled gem is installed.

あとは動作確認です。

バージョンの互換性で動作しなくなるのには気を付けて。

Redmine2.1.2 メール送信設定時のエラー

2012年12月14日 Posted by PURGE

Redmineをアップグレードしてみたが、画面上では下記メール送信のエラー。

hostname was not match with the server certificate

ポイントは、enable_starttls_auto: false を付け足すこと。
まあ、それぞれの環境のsmtpサーバの設定によるのだけどね。

production:
  email_delivery:
    delivery_method: :smtp
    smtp_settings:
      address: "smtp.whoocus"
      port: 25
      authentication: :none
      enable_starttls_auto: false
      domain: 'whoocus.com'

Rails3でjQueryMobileのsubmitが効かない。

2012年12月1日 Posted by PURGE

jQueryMobileで、submitしたのだが、なぜか上手くいかない。
formの属性に、data-ajax = “false” と指定しなければいけないらしい。
これは、どちらかと言うと jQueryMobileの仕様です。

ハマってので覚え書き。

HTML Form

<%= form_tag(item_list_path, 
	    :method => "GET", 
	    :id => "frm", 
	    "data-ajax" => false) do %>
	<fieldset data-role="controlgroup" 
		  data-type="horizontal" 
		  data-mini="true">
	<%= radio_button_tag :item, 1, true, {:class => "item"} %>
	<label for="game_id_1"> 商品1 </label>
	<%= radio_button_tag :item, 2, false, {:class => "item"} %>
	<label for="game_id_2"> 商品2 </label>
	<%= radio_button_tag :item, 3, false, {:class => "item"} %>
	<label for="game_id_0"> 商品3 </label>
	</fieldset>
<% end %>

JavaScript

<script>
	$(function() {
		$(".item").change(function(){
			$("#frm").submit();
		});
	});
</script>