我有三个模型定义如下:
class Comic < ActiveRecord::Base
has_many :feeds
end
class Feed < ActiveRecord::Base
belongs_to :comic
has_many :filters
end
class Filter < ActiveRecord::Base
belongs_to :feed
end
在过滤器表单中,我有以下内容:
<%= f.collection_select :feed_id, Feed.all, :id, :url, { :include_blank => "Please select" } %>
假设我的漫画模型中有一个:name
字段,如何在选择中的Feed网址中显示该字段?
我希望渲染的HTML看起来像这样:
<option value="1">Comic Name - Feed URL</option>
谢谢!
答案 0 :(得分:1)
一个想法是在Feed类中创建一个新的实例方法,用于显示漫画名称和feed的url:
class Feed < ActiveRecord::Base
def display_text
"#{comic.name} - #{url}"
end
end
<%= f.collection_select :feed_id, Feed.all, :id, :display_text, { :include_blank => "Please select" } %>