Shoken Startup Blog

KitchHike Founder/CTO

jQuery UI Dialogでクローズボタンのxが出なくなった

f:id:sfujisak:20140523153425p:plain

ボタンは見えるが、xが出なくなった。

解決策

stackoverflowで見つけたこのやり方を試したら、見えるようになった。原因はわからず。 javascript - jQuery UI Dialog - missing close icon - Stack Overflow

bootstrap.jsとjquery-ui.jsの読み込みで、bootstrap.jsが上に来るように修正する。

application.js

修正前

... 
//= require jquery.ui.datepicker
//= require jquery.ui.dialog
//= require twitter/bootstrap
...

修正後

... 
//= require twitter/bootstrap
//= require jquery.ui.datepicker
//= require jquery.ui.dialog
...

bootstrapとjqueryで変数が重複していたりするのだろうか。

Mongoidで日付の期間を指定して検索する方法

RailsからMongoidを使って、日付の期間を指定して検索する方法。

環境

  • Rails 4.1
  • Ruby 2.1.0
  • Mongoid 4.0.0.beta1
  • MongoDB 2.4.8

3日以内に作成されたユーザーを検索

この3つの書き方ができる。

User.where( :created_at => { "$gte" => Time.now.ago(3.days) }
User.where( :created_at.gte => Time.now.ago(3.days) )
User.gte( :created_at => Time.now.ago(3.days) )

他の条件を追加して絞り込むパターン

localeがjaで、3日以内に作成されたユーザーを検索

User.where( :locale => 'ja' ).gte( :created_at => Time.now.ago(3.days) )

公式ドキュメント

Mongoid: Querying

MongoDBでcollectionを削除する方法

MongoDBでコレクションを削除するにはdrop()を使います。

[root@xxx ~]# mongo
MongoDB shell version: 2.4.5
connecting to: test

> db.students.insert({"name":"shoken"}) ## studentsコレクションを作成
> show collections
students
system.indexes
> db.students.drop()
true
> show collections
system.indexes            ## studentsコレクションが削除されている

Rails 4にアップグレードするとassets:precompile しても minify されない

f:id:sfujisak:20140520215417p:plain

問題

Rails 3 ではassets:precompileすると、jsファイルの連結 + minifyされて改行や空白が削除されていたのだが、Rails 4にすると連結はされるがminifyされなくなった。

解決策

Sprocketsのキャッシュをクリアする。

bundle exec rake tmp:cache:clear

public/assets/*.jsを削除するか、config/environments/production.rb でconfig.assets.js_compressorを変更する。

rm public/assets/*js

その後、assets:precompileでjsファイルが作成されていることを確認する。

$ bundle exec rake assets:precompile RAILS_ENV=production --trace
** Invoke assets:precompile (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Execute assets:precompile
I, [2014-05-20T10:36:09.047520 #20568]  INFO -- : Writing /home/rails/public/assets/application-311212e42fdbdedd7b0af4927xxxxxxx.js

補足: js_compressorはどれを使えば良い?

Rails Asset Pipelineのconfig.assets.js_compressorを変更してみた。

http://guides.rubyonrails.org/asset_pipeline.html#javascript-compression

Googleが提供しているWebサイトのアクセス速度を解析してくれるPageSpeed Insightsで調査した。 結果はuglifierが一番良かった。

  • uglifier : 一番圧縮率が高い。PageSpeedでは合格となった。
  • closure : Googleが作っているが、PageSpeedではあと2%圧縮できますという結果に。
  • yui : Yahooが作っている。手元のMac OSXでエラーになるので今回は未調査。Linuxではコンパイルできた。

参考

How to clear Sprockets cache? - Stack Overflow

Rails で assets:precompile しても minify されない場合の対処 - ichiroc subset

Rails + New RelicでRuby 2.0にバージョンアップするとエラー NoMethodError undefined method `empty?'

解決するまでに時間がかかったので、情報共有。

以下に当てはまる場合は一読を

  • Rails 3.2系
  • New Relicで監視している
  • Ruby 1.9.3から2.0へバージョンアップするとエラー
  • Production環境のみで、TOP画面アクセスした場合にNoMethodErrorが出る
NoMethodError (undefined method `empty?' for nil:NilClass):
  app/controllers/xxxx

解決策はNew Relic Gemのアップデート

New RelicのGemを3.6.x以上にアップデートする。 Gem名はnewrelic_rpm

なぜ解決に時間がかかるのか

理由1. Production環境かつ、New Relicで監視している環境でしか再現しない

原因がわかった後から考えると当然なのだが、原因不明だった時はなぜ本番環境のみで発生するのかわからなかった。Production環境で起動しているステージング環境を用意していたのだが、New Relicを入れてなかったので再現せず、現象の再現までに時間がかかった。

理由2. エラーログにNew Relicとわかる情報が出ない

RailsのログにはNoMethodErrorが出るがスタックトレースにNew Relicの文字が出ない。こんなログが出る。

[2013-12-28 09:12:54] [xxx.xxx.xxx.139] [nil] [xxxxxxxxx85be8a473f10507bd9acc01] Started GET "/" for 50.31.164.139 at 2013-12-28 09:12:54 +0000
[2013-12-28 09:12:54] [xxx.xxx.xxx.139] [nil] [xxxxxxxxx85be8a473f10507bd9acc01] Processing by TopController#index as */*
[2013-12-28 09:12:54] [xxx.xxx.xxx.139] [nil] [xxxxxxxxx85be8a473f10507bd9acc01] Completed 500 Internal Server Error in 280ms
[2013-12-28 09:12:54] [xxx.xxx.xxx.139] [nil] [xxxxxxxxx85be8a473f10507bd9acc01]
NoMethodError (undefined method `empty?' for nil:NilClass):
  app/controllers/concerns/mobile_view_selectable.rb:32:in `render_to_body_with_rename'
  app/controllers/application_controller.rb:36:in `set_timezone'

最初はset_timezoneを追っかけてた。 他の人のブログを読むと、スタックトレース部分のエラーログは環境によって変わる。

理由3. エラーログで検索しても情報が見つけにくい

Ruby 2.0にバージョンアップすると出るので、最初は”Rails Ruby 2.0 NomethodError”や”Rails Ruby 2.0 Error set_timezone”で検索していたが、他の多くの情報に埋もれて解決できる情報にはたどり着けなかった。結局、本番環境でデバッグして、ソースコードを追っていくとNew Relicのコード内でメソッドを呼び出し例外が発生していることが判明。

Rails Ruby 2.0 New Relic”で検索すると困っている人や解決した人がいっぱいいた。