railscast#196,197 Nested Model Formをrails4で動かす

http://t-taira.hatenablog.com/entry/20110420/1303310783
を元にしてstrong parameterがあるとうまく動かないのでしょうがなく

    def category_params
      params.require(:category).permit!
    end

してやり、DEPRECATION WARNINGが今後のことを考えると怖いので

DEPRECATION WARNING: link_to_function is deprecated and will be removed from Rails 4.1. We recommend using Unobtrusive JavaScript instead. See http://guides.rubyonrails.org/working_with_javascript_in_rails.html#unobtrusive-javascript. (called from link_to_remove_fields at /Users/seijirou/Dropbox/code/ruby/rails4/Nested/app/helpers/categories_helper.rb:3)
    #link_to_function(name, raw("add_fields(this, \'#{association}\', \'#{escape_javascript(fields)}\')"))
    link_to name ,'javascript:void()',:onclick => ("add_fields(this, \'#{association}\', \'#{escape_javascript(fields)}\')" )

としてやるととりあえず動作した。これはかなり使うことになる。
参考:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
http://t-taira.hatenablog.com/entry/20110420/1303310783
http://stackoverflow.com/questions/15919761/rails-4-nested-attributes-unpermitted-parameters

systemuのハマりどころ

会社でちょっと話題になった。上司が解決してくれたのでメモしておく。
プロセスが長く掛かりそうなときに途中で殺す処理を入れることがある。
https://github.com/ahoward/systemuによると

      require 'systemu'
begin    
      looper = %q( ruby -e" loop{ STDERR.puts Time.now.to_i; sleep 1 } " )
    
      status, stdout, stderr =
        systemu looper do |cid|
          sleep 3
          # <この辺>
          Process.kill 9, cid
          
        end
    
      p status
      p stderr
rescue => ex # <例外キャッチ>
  # 例外処理
end

こんなソースコードがある。
問題なのは<この辺>で例外が発生した時にどうなるかだ。
<例外キャッチ>でキャッチできない。
do-endブロックの中でbegin-endしてやらないとダメ。

railsメモ railsで別DBを利用する

既存のユーザーテーブルなどを利用したいときがある。その場合は以下の様にすると良い

class Hoge < ActiveRecord::Base
  establish_connection(
                       :adapter  => "mysql2",
                       :host     => "ホスト",
                       :database => "データベース名",
                       :username => "ユーザー",
                       :password => "パスワード"
                       )
  self.table_name = "table名"
  self.primary_key = "プライマリーキー"
end

rails4で確認

hoge_db:
  adapter: mysql2
  encoding: utf8
  database: dbname
  username: user
  password: password
  host: host

という風にdatabase.ymlに追記していた場合は

class User < ActiveRecord::Base 
  establish_connection(:hoge_db)

end

となるし、共有したい場合は

class LegacyBase < ActiveRecord::Base 
  establish_connection(:legacy_db)
end

@app/models/user1.rb
class User1 < LegacyBase 
end

@app/models/user2.rb
class User2 < LegacyBase 
end

で良いらしい。
参照: http://rails3try.blogspot.jp/2011/06/rails3-db.html

study duoというサービスを作った

会社でDUOが流行っている。
http://modeverv.aa0.netvolante.jp/study_duo/html/
にて。
tmuxなどで使えるようにperlAPIも書いた。
検索は無いけど
http://modeverv.aa0.netvolante.jp/study_duo/api/100/en
で100番の英文が、
http://modeverv.aa0.netvolante.jp/study_duo/api/100/jp
で100番の日本語が出る。
正しいperlでの書き方わからない。JSONとCGIというモジュールを使った。
あしたperlの人に正しいperl教えて貰う予定。
https://github.com/modeverv/study_duo

rails4のサブディレクトリ運用

http://quickhack.net/nom/blog/2012-09-19-rails-with-relative-url-root.html#sec-6

config.ruは

map ActionController::Base.config.relative_url_root || "/" do
  run Rails.application
end

起動は

RAILS_RELATIVE_URL_ROOT='/yourdict' unicorn -p 8080 -E production -D

apacheとかは

ProxyPass /yourdict http://localhost:8080/yourdict
ProxyPassReverse /yourdict http://localhost:8080/yourdict

で。