rails:2055
From: "YOKOYAMA, Hideyuki" <yokoyama@c...>
Date: Fri, 02 Mar 2007 00:51:24 +0900
Subject: [rails:2055] Re: コード値の表示時の変換方式
横山です
いつもお世話になっております
moriq 様 ありがとうございます
試してみましたが
実装が間違っているのか
意図した動きになりませんでした
controller で
class MusicController < ApplicationController
def show
@m = Music.find(:first)
end
end
view で
<%= @p.gender %> <-- "#" と表示される(オブジェクトID?)ほんとは"1"になってほしい -->
<%= @p.gender.to_desc %> <-- "邦楽"と表示される 意図どおり -->
<%= @p.gender.gender %> <-- "1"と表示される -->
という感じです
うーん
|moriqです。
|
|YOKOYAMA, Hideyuki wrote:
|> なんらかの方法で上記の
|> genre.to_desc
|> は定義可能なんでしょうか?
|
|composed_ofでできると思います。
|
|class Music < ActiveRecord::Base
| composed_of :genre
|end
|
|class Genre
|GENRES = {
| 1 => "邦楽",
| 2 => "洋楽",
| 3 => "クラシック",
| 4 => "その他"
|}.freeze
|
| def initialize(id)
| @id = id
| end
|
| def genre
| @id
| end
|
| def to_desc
| GENRES[@id]
| end
|end
/*----
横山 秀行
YOKOYAMA, Hideyuki <yokoyama@c...>
〒100-0005 東京都千代田区丸の内1−9−1 EA207号
TEL:???-????-????
-- PGP PUBLIC KEY SERVER URL --
http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x089F75BE
----*/
--
ML: rails@r...
使い方: http://QuickML.com/
2041 2007-02-28 05:47 [hisano@s... ] 多段のlayoutの作成方法 2042 2007-02-28 08:03 ┗[moriq@m... ] 2043 2007-02-28 09:01 ┗[hisano@s... ] 2044 2007-02-28 17:50 ┗[yokoyama@c... ] コード値の表示時の変換方式 2045 2007-02-28 18:09 ┣[shachi@j... ] 2046 2007-02-28 19:06 ┗[utsumi@t... ] 2047 2007-02-28 22:35 ┗[yokoyama@c... ] 2048 2007-02-28 23:52 ┣[utsumi@t... ] 2049 2007-03-01 00:15 ┃┣[utsumi@t... ] 2052 2007-03-01 00:24 ┃┃┗[yokoyama@c... ] 2050 2007-03-01 00:19 ┃┗[yokoyama@c... ] 2051 2007-03-01 00:23 ┗[moriq@m... ] 2053 2007-03-01 00:30 ┣[moriq@m... ] -> 2055 2007-03-01 16:51 ┗[yokoyama@c... ] 2056 2007-03-01 18:59 ┗[moriq@m... ]