link_to で内部にタグがあったりする時はブロックを使いましょう

Zurb-Foundation とか Twitter bootstrap を使ってると、アイコンをリンク内に含めたくなったりしますよね。こんな感じに [crayon lang=“html”] Reload [/crayon] でもこれをそのまま `link_to` で囲んで [crayon lang=“html”] <%= link_to “ Reload”,“http://example.com/reload" %> [/crayon] とすると``タグが HTML エスケープされて悲しい事態になるんです。 対策 なのでそういう時は `link_to` さんはブロックが取れるのでブロックを使って処理しましょう。ブロックの返り値がリンクのテキストになります。 [crayon lang=“html”] <%= link_to “http://example.com/reload", class: ‘reload’ do %> Reload <% end %> [/crayon] ちなみに `button_to` も同じような関数なのでこういう風にかけます。 [crayon lang=“html”] <%= button_to “”, class: ‘small radius’ do %> Reload <% end %> [/crayon] これ知らない時に必死に HTML エスケープされないために何をすればよいのかを探しまくったのは今では良い思い出でもなんでもありません。時間返せ。 参考 [ruby on rails – link_to in helper with block – Stack Overflow][1] ...

2013-12-11 · 鉄

既に存在するindexにunique制限をかけるmigrationの書き方。

そのまま unique 条件付の index を追加しようとするとエラーになるので まず既にある index を削除してから追加しましょう。 [crayon title=“db/migrate/add_unique_constraint.rb”] def change remove_index :editabilities, [:user_id, :list_id] add_index :editabilities, [:user_id, :list_id], unique: true end [/crayon] 参考 [ruby on rails – How to add unique constraint to already existing index by migration – Stack Overflow][1]

2013-12-09 · 鉄

RailsでModelが不規則名詞だった場合のControllerの命名規則

Rails の Model は常に単数名詞、そして Controller はその名詞の複数形という規則がありますが、Model が”Child"などの不規則変化名詞だった時に Controller 名はChildren なのか、Childs なのかがわからなかったので調べました。 答え プログラム的には当然簡単に処理できる Childs だと思ったけども実際はちゃんと不規則変化にも対応してるらしく Children が正解です。 その他の不規則名詞 他の不規則名詞は https://github.com/rails/rails/blob/4-0-0/activesupport/lib/active_support/inflections.rb に載っているんですが、 inflect.irregular('person', 'people') inflect.irregular('man', 'men') inflect.irregular('child', 'children') inflect.irregular('sex', 'sexes') inflect.irregular('move', 'moves') inflect.irregular('cow', 'kine') inflect.irregular('zombie', 'zombies') zombie って不規則名詞なの…? cow→kine の変化はそもそも英単語がわかりませんでした、、 Zombie が不規則名詞の理由 ([2013/12/14] 追記) 単数形から複数形への変化は規則通りですが、複数形から単数形への変化 zombies -> zombie が不規則で、規則通り変化させてしまうと zombies -> zomby になってしまうから不規則名詞らしいです。英語は難しいですね。 情報元 [Model and Controller's naming rule for irregular noun in Rails – Stack Overflow][1]

2013-05-21 · 鉄

Railsで新規プロジェクトを作ろうとすると`expand_path’: non-absolute homeというエラーが出る

rails new hoge とすると `expand_path': non-absolute home というエラーが出てしまって「Windowsだしなあ。」と諦めかけてたけど、このhomeというのは環境変数のHOMEのことらしく、確かにcygwinを使う時のためにHOMEに /cygdrive/ から始まる値を入れてたのを思い出したので環境変数 HOME を削除してみたら動いた。 ちなみに irb もHOMEに入ってる値によっては動かないらしい。 参考 `expand_path': non-absolute home http://devnet.jetbrains.com/thread/437313

2013-05-01 · 鉄