カテゴリー : Ruby

[Ruby on Rails] DB を初期化・リセットする方法 ~ rake db:reset と rake db:migrate:reset

Ruby on Rails で、データベース (DB) リセットする方法を2通りご紹介します。

Ruby on Rails

  1. rake db:reset
  2. rake db:migrate:reset

続きを読む

[Ruby on Rails] favicon_link_tag で favicon.ico を設定する方法

Ruby on Rails の favicon_link_tag ヘルパーメソッドを使って favicon.ico を設定する方法をご紹介します。

Ruby on Rails

続きを読む

[Ruby on Rails] 常に HTTPS 接続する設定は config.force_ssl = true

Ruby on Rails にて、常に HTTPS/SSL 接続する設定方法をご紹介します。

Ruby on Rails

続きを読む

[Ruby on Rails] erb2haml を使って .erb ファイルを .haml へ変換する

Ruby on Rails で erb2haml を使って .erb ファイルを .haml へ変換したので、その方法をご紹介します。

Ruby on Rails

続きを読む

[Ruby] ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed. の解決方法

Ruby on Rails 5 を bundle install でインストール中に libxml2 が無くて nokogiri がインストールできないというエラーが発生しました。

MacOS に nokogiri のインストールに必要なライブラリがインストールされてなかっただけなのですが、無事に解決できたのでその方法をご紹介します。

Ruby lang

続きを読む

[RVM] Empty path passed to certificates update, functions stack: requirements_osx_update_openssl_cert_run …

rvm で Ruby version 2.1.3 をインストールすると Empty path passed to certificates update 的なエラーが発生しました。

% rvm install 2.1.3
 
Searching for binary rubies, this might take some time.
Found remote file https://rubies.travis-ci.org/osx/10.9/x86_64/ruby-2.1.3.tar.bz2
Checking requirements for osx_brew.
Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.
Requirements installation successful.
ruby-2.1.3 - #configure
ruby-2.1.3 - #download
ruby-2.1.3 - #validate archive
ruby-2.1.3 - #extract
ruby-2.1.3 - #validate binary
ruby-2.1.3 - #setup
ruby-2.1.3 - #gemset created /Users/bakorer/.rvm/gems/ruby-2.1.3@global
ruby-2.1.3 - #importing gemset /Users/bakorer/.rvm/gemsets/global.gems......................................................................
ruby-2.1.3 - #generating global wrappers..............
ruby-2.1.3 - #gemset created /Users/bakorer/.rvm/gems/ruby-2.1.3
ruby-2.1.3 - #importing gemsetfile /Users/bakorer/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.1.3 - #generating default wrappers..............
/Users/bakorer/.rvm/scripts/functions/support: line 490: 56810 Trace/BPT trap: 5       "$ruby_path" -rrbconfig -e '\
    File.open("'"$config_path"'","w") { |file|
      RbConfig::CONFIG.sort.each{|key,value|
        file.write("#{key.gsub(/\.|-/,"_")}=\"#{value.gsub("$","\\$")}\"\n")
      }
    }
  ' > /dev/null 2>&1
Empty path passed to certificates update, functions stack: requirements_osx_update_openssl_cert_run rvm_requiremnts_fail_or_run_action __rvm_osx_ssl_certs_ensure_for_ruby __rvm_osx_ssl_certs_ensure_for_ruby_except_jruby external_import_setup external_import main

Ruby 2.1.3 を使おうとすると dyld: Library not loaded: /usr/local/lib/libgmp.10.dylib という libgmp が読み込めないというメッセージが表示されてしまいます。

% rvm use 2.1.3
 
Using /Users/bakorer/.rvm/gems/ruby-2.1.3
dyld: Library not loaded: /usr/local/lib/libgmp.10.dylib
  Referenced from: /Users/bakorer/.rvm/rubies/ruby-2.1.3/bin/ruby
  Reason: Incompatible library version: ruby requires version 13.0.0 or later, but libgmp.10.dylib provides version 12.0.0

下記のように disable-binary フラグを付けて再インストールをすると直るようです。

% rvm reinstall 2.1.3 --disable-binary
 
ruby-2.1.3 - #removing rubies/ruby-2.1.3 - please wait
Checking requirements for osx_brew.
Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.
Requirements installation successful.
Installing Ruby from source to: /Users/bakorer/.rvm/rubies/ruby-2.1.3, this may take a while depending on your cpu(s)...
ruby-2.1.3 - #downloading ruby-2.1.3, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11.4M  100 11.4M    0     0  5118k      0  0:00:02  0:00:02 --:--:-- 5118k
ruby-2.1.3 - #extracting ruby-2.1.3 to /Users/bakorer/.rvm/src/ruby-2.1.3 - please wait
ruby-2.1.3 - #configuring - please wait
ruby-2.1.3 - #post-configuration - please wait
ruby-2.1.3 - #compiling - please wait
ruby-2.1.3 - #installing - please wait
ruby-2.1.3 - #making binaries executable - please wait
Rubygems 2.2.2 already available in installed ruby, skipping installation, use --force to reinstall.
ruby-2.1.3 - #gemset created /Users/bakorer/.rvm/gems/ruby-2.1.3@global
ruby-2.1.3 - #importing gemset /Users/bakorer/.rvm/gemsets/global.gems - please wait
ruby-2.1.3 - #generating global wrappers - please wait
ruby-2.1.3 - #gemset created /Users/bakorer/.rvm/gems/ruby-2.1.3
ruby-2.1.3 - #importing gemsetfile /Users/bakorer/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.1.3 - #generating default wrappers - please wait
ruby-2.1.3 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.1.3 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri
Making gemset ruby-2.1.3 pristine - please wait
Making gemset ruby-2.1.3@global pristine - please wait

以上です。


参考情報

osx – Error when installing Ruby 2.1.3 with RVM – Stack Overflow

[Heroku] Ruby on Rails 4 + Mongoid + MongoHQ で「500 Internal Server Error」

Ruby on Rails 4 + Mongoid + MongoHQ on Heroku という環境で、1stデプロイしたら初っ端から「500 Internal Server Error」に遭遇しました。

エラーログ

 app/web.1:  I, [2014-02-11T01:13:25.785412 #2]  INFO -- : Started GET "/" for 223.132.104.209 at 2014-02-11 01:13:25 +0000 
 app/web.1:  I, [2014-02-11T01:13:25.790612 #2]  INFO -- : Processing by VideosController#index as HTML 
 app/web.1:  I, [2014-02-11T01:13:25.817544 #2]  INFO -- :   Rendered videos/index.html.haml within layouts/application (3.9ms) 
 app/web.1:  I, [2014-02-11T01:13:25.817786 #2]  INFO -- : Completed 500 Internal Server Error in 27ms 
 app/web.1:  F, [2014-02-11T01:13:25.819195 #2] FATAL -- :  
 app/web.1:  ActionView::Template::Error (:consistency is an unknown option. Valid options  :max_retries, :pool_size, :retry_interval, :refresh_interval, :down_interval, :ssl, :timeout, :instrumenter, :auto_discover.): 
 app/web.1:       9:     %th 
 app/web.1:      10:     %th 
 app/web.1:      11:  
 app/web.1:      12:   - @videos.each do |video| 
 app/web.1:      13:     %tr 
 app/web.1:      14:       %td= video.name 
 app/web.1:      15:       %td= video.url 
 app/web.1:    app/views/videos/index.html.haml:12:in `_app_views_videos_index_html_haml___1990342454341184297_70073653532820'

どうやら、conf/mongoid.yml の書き方が良くなかったみたいです。

MongoHQ 向けの動かない conf/mongoid.yml

production:
  sessions:
    default:
      uri: <%= ENV['MONGOHQ_URL'] %>
      options:
        consistency: :strong
        max_retries: 1
        retry_interval: 0

↓ 修正

MongoHQ 向けの正常動作する conf/mongoid.yml

production:
  sessions:
    default:
      uri: <%= ENV['MONGOHQ_URL'] %>

options の何がダメだったのか詳しく追ってないので、時間を見付けて最適なオプションを調べたいです。


参考情報

500 Internal Server Error using MongoLab + Rails 4 + Mongoid on Heroku – Stack Overflow

[Ruby on Rails 4] group :assets do … end は廃止

Ruby on Rails 4 から Gemfile に group :assets do … end と書くのは廃止になったそうです。

Rails 4.0 removed the assets group from Gemfile. You’d need to remove that line from your Gemfile when upgrading.

Ruby on Rails Guides


参考情報

Rails 4はgroup :assetsがなくなった。 – iiirc

Ruby on Rails4 + Mongoid + RSpec

Ruby on Rails4 + Mongoid + RSpec の環境構築メモ。

gem install rails --version 4.0.1
rails new myapp --skip-active-record --skip-test-unit
cd myapp
# Gemfile
gem 'mongoid', git: 'https://github.com/mongoid/mongoid.git'
rails g mongoid:config
      create  config/mongoid.yml
rails g rspec:install
% git diff
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 943bc19..9b7b0ef 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -13,6 +13,19 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
 ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
 
 RSpec.configure do |config|
+  ## Mongoid
+  config.before(:suite) do
+    DatabaseCleaner[:mongoid].strategy = :truncation
+  end
+
+  config.before(:each) do
+    DatabaseCleaner[:mongoid].start
+  end
+
+  config.after(:each) do
+    DatabaseCleaner[:mongoid].clean
+  end
+
   # ## Mock Framework
   #
   # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
@@ -22,12 +35,12 @@ RSpec.configure do |config|
   # config.mock_with :rr
 
   # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
-  config.fixture_path = "#{::Rails.root}/spec/fixtures"
+  # config.fixture_path = "#{::Rails.root}/spec/fixtures"
 
   # If you're not using ActiveRecord, or you'd prefer not to run each of your
   # examples within a transaction, remove the following line or assign false
   # instead of true.
-  config.use_transactional_fixtures = true
+  # config.use_transactional_fixtures = true
 
   # If true, the base class of anonymous controllers will be inferred
   # automatically. This will be the default behavior in future versions of
diff --git a/config/application.rb b/config/application.rb
index 7c9e566..9694074 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -4,7 +4,7 @@ require File.expand_path('../boot', __FILE__)
 # require "active_record/railtie"
 require "action_controller/railtie"
 require "action_mailer/railtie"
-require "sprockets/railtie"
+# require "sprockets/railtie"
 # require "rails/test_unit/railtie"
 
 # Require the gems listed in Gemfile, including any gems
diff --git a/config/initializers/mongoid.rb b/config/initializers/mongoid.rb
new file mode 100644
index 0000000..e400231
--- /dev/null
+++ b/config/initializers/mongoid.rb
@@ -0,0 +1 @@
+Mongoid.load!(Rails.root.to_s + "/config/mongoid.yml")

参考情報

Mongoid: Installation

Getting Started Rails 4 with MongoDB | Just Some More Dev Ideas

Using Mongoid with Rspec

Mongoid: Installation [Configuration]

[RVM] .rvmrc から .ruby-version と .ruby-gemset に移行すべき

RVM で .rvmrc ファイルで Ruby のバージョンと gemset を切り替えていたのですが、今だとWarningメッセージが表示されるんですね。

You are using '.rvmrc', it requires trusting, it is slower and it is not compatible with other ruby managers,
you can switch to '.ruby-version' using 'rvm rvmrc to [.]ruby-version'
or ignore this warnings with 'rvm rvmrc warning ignore /Users/bakorer/git/myapp/.rvmrc',
'.rvmrc' will continue to be the default project file in RVM 1 and RVM 2,
to ignore the warning for all files run 'rvm rvmrc warning ignore all.rvmrcs'.
 
Using /Users/bakorer/.rvm/gems/ruby-2.0.0-p247 with gemset myapp

どうやら、これからはRubyのバージョン管理システム(rvm, rbenv, rbfu)で設定ファイルを統一する動きがあったそうで、下記のように .ruby-version と .ruby-gemset に分割しておいた方がよさそうです。

.rvmrc ファイル

rvm use ruby-2.0.0-p247@mygemset

↓ .rvmrc を2ファイルに分割します。

.ruby-version ファイル

ruby-2.0.0-p247

.ruby-gemset ファイル

mygemset

もちろん、従来の .rvmrc ファイルも使えるので、その場合、警告を無視するには下記のコマンドを実行すればOKです。

rvm rvmrc warning ignore all.rvmrcs

参考情報

Use rvmrc or ruby-version file to set a project gemset with RVM? – Stack Overflow

.rvmrcから.ruby-gemsetと.ruby-versionに移行 – Qiita [キータ]

Rubyバージョン管理界にも統一球導入?