collection_select
手册中 的说明:
collection_select (object, method, collection, value_method, text_method, options = {}, html_options = {})
没有详细提到selected的问题,往下到 select 方法的用法:
select (object, method, choices, options = {}, html_options = {})
其中有提到selected的用法:
By default, post.person_id is the selected option. Specify :selected => value to use a different selection or :selected => nil to leave all options unselected.
也就是说,这样写:
collection_select(:album, :id, albums, :id, :name}
它会自动比较@album的id方法的值
现在需要的做关联处理,需要这样构造:
collection_select(:photo, :album_id, albums, :id, :name, {:selected => @album} )
但不起作用。
这里 和 这里 都说修正了,但实际测试都不行, WIKI里也说不行
非官方几个select helper的详细介绍
后来想想,其实关联实现其实只是要求select的id 和 name符合规则就行了,可以这样hard code:
collection_select(:album, :id, albums, :id, :name, {}, {:id => 'photo_album_id', :name => 'photo[album_id]'} )
问题解决。
查看更多关于collection_select的详细内容...