map.resources の整理
map.resources :estates, :has_many => [:rooms, :water_meters]
は
map.resources :estates do |estate| estate.resources :rooms estate.resources :water_meters end
とも書けます。
私は後者のほうで統一する事で頭の中が少しスッキリしました。
selectメソッドと selected について
ググったらいくつか記事がありましたが、私なりに書いてみたいと思います。
オブジェクトの型が一致していないとデフォルト表示(selected)してくれません。
migration抜粋
t.date :kiboubi
view抜粋
<%= content_tag :th, "配達希望日" %> <%= content_tag :td, f.select(:kiboubi, (7.day.since.to_date .. 17.day.since.to_date).collect{|d| [d.to_s(:ja), d]}, {:include_blank => '(なし)'}) -%>
selected がうまくセットされない人は型が一致しているか注意してください。
[d.to_s(:ja), d] の部分の d がポイントです。
ついつい d.to_s とかやっちゃってハマりました。なんかHTMLに出力されるのだから String かなと。変な錯覚(私だけ!?)
exception_notification を rails2.3 に入れる
script/plugin install で入れるとハマる。Rails3 用になってるからね。
- https://github.com/rails/exception_notification/tree/2-3-stable から tarball をGet!
- vendor/plugins/exception_notification として解凍
後は
config/environment.rb に
ExceptionNotification::Notifier.exception_recipients = %w(foo@example.jp)
と追加して、ドキュメントに従い
app/controllers/application_controller.rb に
include ExceptionNotification::Notifiable
です。
配列の中身が2個なのに first, last 共に同じインスタンス!?
Rails のバグなのでしょうか・・・?
誰か教えてください。
script/console の実行結果です。
>> Entry.find(3593).users.class => Array >> Entry.find(3593).users.collect(&:id) => [2153, 2124] >> Entry.find(3593).users.first.id => 2153 >> Entry.find(3593).users.last.id => 2153 >> Entry.find(3593).users[0].id => 2153 >> Entry.find(3593).users[1].id => 2124
なぜ、last が 2153 を返してしまうのでしょうか?
Rails バージョンは 2.1.2 です。
Capistranoのメンテナンスモードの RailsBaseURI版
使用環境
- passenger 2.2.15
- capistrano 2.5.19
cap deploy:web:disable
するとメンテナンスモードになりますね。メンテナンスモード中にアクセスすると以下の様な画面になります。
私は一つのサーバに複数のRailsアプリを運用しているので、
RailsBaseURI を使っています。
以下の様にして出来ました。
<VirtualHost *:80> ServerName myserver DocumentRoot /var/www/html RailsBaseURI /myproj <Directory /var/www/html/myproj> ErrorDocument 503 /system/maintenance.html RewriteEngine On RewriteCond %{REQUEST_URI} !.(css|gif|jpg|png)$ RewriteCond /var/www/html/myproj/system/maintenance.html -f RewriteCond %{SCRIPT_FILENAME} !maintenance.html RewriteRule ^.*$ - [redirect=503,last] </Directory> RailsBaseURI /redmine </VirtualHost>
.htaccess を使う場合、どこに置いたらいいかわかりません :-
dbに保存した画像を表示すると途中で切れてる...
引き続き Attachment_fu + db_files の話題です。
rails 2.3 + MySQL を使っているのですが、DBに保存した画像データを原寸大表示すると途中で途切れてる・・・。
なんで?
ブラウザから画像ダウンロードして、ファイルサイズみると 64K だ。
じゃ、元々はどうなのか?
$ ruby script/console
Loading development environment (Rails 2.3.10)
>> Photo.first.current_data.size
=> 65535
ふむ。じゃスキーマ定義だな。
・
・
・
ビンゴ!
migration で binary を指定すると blob なのね。
すなわち、binary だと 64K なのか。使えねー!
で、以下になりました。
class CreateDbFiles < ActiveRecord::Migration def self.up create_table :db_files do |t| t.binary :data, :null => false, :limit => 10.megabyte # limit 指定しないと 64K になっちゃうよ end end def self.down drop_table :db_files end end
これで mediumblob になりました。
ThankYou. http://memo.yomukaku.net/entries/59