所选选项不适用于选择

Joh*_*ohn 5 ruby-on-rails-3

我有这个选择哪个工作正常,但默认选择为空并且不显示所选值(正确填充):

<%= f.select(:relationgroup, options_for_select(@relationgroups), { :selected => @relation.relationgroup, :include_blank => true}) %>
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?谢谢!

KL-*_*L-7 6

试试这样:

<%= f.select(
        :relationgroup, 
        options_for_select(@relationgroups, @relation.relationgroup), 
        :include_blank => true
) %>
Run Code Online (Sandbox Code Playgroud)

不确定,但也许它会更好.

无论如何,假设Relationgroup某些模型带有idname(或任何其他属性,您希望在选择选项中可见)属性,并且您relationgroup_id在模型中使用默认外键,您最好构建您的选择:

<% f.select(
       :relationgroup_id,
       options_from_collection_for_select(@relationgroups, :id, :name),
       :include_blank => true
) %>
Run Code Online (Sandbox Code Playgroud)

它会根据选择选择的值object.relationgroup_id这里object是你建立表单模型.有关更多信息,请参阅文档.