rails

2.1.0でmigrationがかわった

参考サイト Rails 2.1.0 - migration: Rails開発日記 http://brass.to/blog/rails_migration_rake_task.html Ruby On Rails ピチカート街道 - Rails 2.1・その4(db migration ファイルのバージョンがUTC日付に) - ダウンバージョンのしかたとか rake db:mig…

ちょっとPHPやってるあいだに2.1.0になってる

なぜ、何の気なしに gem update をしてしまったのだろう。反省している。railsが2.0.2から2.1.0になり 作ってる奴が、動かなくなった。。。 参考 rails2.0.2 → 2.1.0 に移行 - 夜の Discovery Rails2.0から2.1への移行を試してみる - ザリガニが見ていた...…

マイグレーションでシリアル型

マイグレーションだけphp案件でつかってて、serialのを追加したかった def self.up add_column :price, :id, :serial add_column :price, :periodtype, :string end で、できたpostgresql8.3でしかみてない。

error_messages_for

めも。 # error_messages_for 'user' # # To specify more than one object, you simply list them; optionally, you can add an extra +object_name+ parameter, which # will be the name used in the header message. # # error_messages_for 'user_commo…

route.rb デフォルトは一番下か、、JK

route.rbの # Install the default routes as the lowest priority. map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'は、この下に色々書いても動くけど、will paginateのpostなときに、namespaceとか全部シカトされた…

QRコード

参考が消えかかってるので。 このページは、G o o g l e で 2008年4月9日 05:28:21 GMT に保存された http://www.fraction.jp/log/archives/2006/10/1035 のキャッシュです。 G o o g l e のキャッシュとはウェブ クロール時に保存したページのスナップショ…

find_by_sqlの名前付きプレースホルダ

find_by_sqlのときは sql = "select * from hoge where fuga = ? and moge = ?" fuga = 1 moge = 2 Hoge.find_by_sql([sql, fuga, moge]) のプレースホルダに名前をつけたいとき cond = Hash.new sql = "select * from hoge where fuga = :fuga and moge = :…

DateHelperのオプション

参考 例:hoge', 'hage',{:discard_hour => true, :use_month_numbers => true } %> 1. :order => [:day,:month,:year ] # 選択する項目の順番を入れ替える 2. :discard_year => true # 年のセレクトボックスを削除する。 3. :discard_month => true # 月以…

テストガバレッジツールrcov

テストを書いてみたので、ついでにいれてみた。 gem install rcov 使ってみる rcov -x /opt/rubygems --rails test/**/*_test.rb -x /opt/rubygemsでgemのとかをはずす --railsでrailsのをはずす

ActiveScaffold入れてみた

ユーザーテーブルがあって、モデルもあってという前提で インスコ ./script/plugin install http://activescaffold.googlecode.com/svn/tags/active_scaffold/ 管理エリア作る ユーザー管理でも作ってみる routes.rbにルート情報書く/adminで使いたい #管理…

xenで作ってもらった新しい鯖でrails+mysql入れる

rubyダウンロード yumがruby1.8.5しかないみたい。 1.8.6じゃないとGetTextとRails2.xでなんかよくないとかどっかで見たので、1.8.6が入れたい。 なので、落としてきて入れる。 wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p114.tar.bz2 wget htt…

カラーピッカー

http://www.knallgrau.at/code/colorpicker を使う。 ダウンロード wget http://www.knallgrau.at/code/colorpicker/files/colorpicker.zip 配置 public + images + blank.gif + hline.png + hue.png + picker.png + pickerbg.png + select.gif + javascript…

GetTextで日本語化

environment.rbの最後に require 'gettext/rails' application.rbに init_gettext 'hogehogemogemoge'でとりあえず。 他にも色々あるので以下参考あとで書く http://www.yotabanana.com/hiki/ja/ruby-gettext-howto-ror.html Ruby On Rails ピチカート街道 -…

modelに定数書きたい

modelに定数が書いておけると便利なときもあると思う class Moge < ActiveRecord::Base @@hoge = "hogehoge" @@fuga = "fugafuga" cattr_accessor :hoge, :fuga confにconfigファイル的なyamlを置いておいてアプリで使いたい application.rbで $app_config =…

既存dbからmigrationファイルを作る

rake db_schema_dumpで、db/schema.rbができあがる。 参考 http://tam.qmix.org/wiki/Migration.html

migrattionでnot null

add_column :hoge, :fuga, :integer, {:null => false} だと、 rake aborted! PGError: ERROR: column "figa" contains null values : ALTER TABLE hoge ALTER "fuga" SET NOT NULLと怒られた add_column :hoge, :fuga, :integer, {:null => false, :default…

jpmobileでezのシミュレータでtrans_sidを通るように

OpenwaveSDKで確認してたら、 !request.mobile.supports_cookie?がfalseなのに、 シミュレータの方ではcookie食えなかった。 ので、場当たり的に if transit_sid_mode == :mobile if request.mobile? if cookies.blank? return true else return !request.mo…

jpmobile trans_sidが効かないとき

viewで <%= link_to :hoge, {:action => :fuga} %>なときは、 <a href="/xxx/fuga?_sid=098731246hdfa9s8hdf">hoge</a> みたいになるけど名前付きルートで <%= link_to :hoge, fuga_path(@fuga) %>とかのように、名前指定で引数を渡したときだけだと、 def link_to(name, options = {}, html_options = nil) ur…

ARの関連名とか

has_many :hoge_groups, :order => :positionじゃなくて has_many :groups, :class_name => "HogeGroup", :order => :positionでも、おk 命名規約に従うべきかもだけど、xxxxx_xxxxxx_xxxxx_groupsとかウザ過ぎる。

実行時にvalidatesを追加する

あとづけなvalidatess Hoge.validates_inclusion_of :detail, :in => %w{ 2 3 } fuga= Hoge.new(:foo=> params[:hoge][:foo] ) detail.valid? params[:hoge][:foo]は1だとfalse params[:hoge][:foo]は2だとtrue これ駄目だわ あとのが全部バリデーション効い…

CSV書き出し

report = '' CSV::Writer.generate(report, ',') do |csv| csv << ["ログイン名 ", "パスワード"] User.find_all.each do |model| csv << [model.login, model.password] end end send_data(NKF.nkf('-U -s -Lw', report), :type => 'text/csv; charset=shif…

requestオブジェクト

参考 Rails開発日記 AUTH_TYPE : request.auth_type CONTENT_LENGTH : request.content_length CONTENT_TYPE : request.content_type GATEWAY_INTERFACE : request.gateway_interface PATH_INFO : request.path_info PATH_TRANSLATED : request.path_transla…

2.0でwill_paginateを使う

script/plugin install svn://errtheblog.com/svn/plugins/will_paginate 参考 m(__)m http://brass.to/blog/will_paginate.html AutoPagerizeなwill_paginateにしておく - ザリガニが見ていた...。 will_paginate - happy lie, happy life will_paginate の…

scaffoldとかの本体

gemディレクトリ/rails-2.0.2/lib/rails_generator/generators/components

hじゃないサニタイズ

<%=h @hoge %> だと、けっこう問答無用でエスケープされる。 でも、ある程度のHTMLは入力許したいとき、タグと属性をホワイトリストで指定できた。 <%= sanitize %(<table><tr><td><font id="hoge" style="color:red;">ほげ</font></td></tr></table>), :tags => %w(font), :attributes => %w(class style) %> で表示されるのは <font style="color:red;">ほげ</font> だけ…

date_selectにclassをつけたい

プラグイン作ってその中で module ActionView module Helpers module DateHelper #中略 def select_html(type, html_options, options) case type when "year" then html_class = %(class = "#{options[:yclass] || "date_select"}") when "month" then html…

railsでjavascriptにrubyの値を引数で渡す

<% hoge = "hogehoge" %> <%= javascript_tag(%( window.onload=function default() { alert("#{hoge}"); } )) %>

rails2からはacts_as_*はインストールしないといけない。

ruby script/plugin install acts_as_list

多対多なモデル

class Hoge< ActiveRecord::Base has_many :hoges_fugas_assigns has_many :fugas, :through => :hoges_fugas_assigns, :source => :wawawa_fuga #以下略 class WawawaFuga< ActiveRecord::Base has_many :hoges_fugas_assigns has_many :hoges, :through =>…

route.rbのこと

/admin/hoge/1/fuga/hoo/wawawaとかしたいとき map.namespace :hoge do |hoge| hoge.resources :fuga do |fuga| fuga.resources :hoo, :member=> [:wawawa] end end 参考 http://d.hatena.ne.jp/zariganitosh/20080203/1202091772