1

None of the below select helpers are working only for the selected part:

<%= f.select(:project_name, @parent_projects.collect {|p| [ p.name, p.name ] }, {:include_blank => 'Select Project'}, {:selected => "#{Project.find_by_identifier(params[:parent_id]).name}"}) %>

OR

<%= f.select(:project_name, @parent_projects.collect {|p| [ p.name, p.name ] }, {:include_blank => 'Select Project'}, :selected => "#{Project.find_by_identifier(params[:parent_id]).name}") %>

I am getting no error message but the the select box is not pre-selecting the name.

Checked the API from here

4

1 回答 1

1

请参阅官方文档select- 该方法具有您需要的更多魔力;它更多地用于为特定模型/属性构建选择列表。由于您是手动构建零件,因此您应该使用更直接的select_tag方法。

select_tag('project_name',
    options_from_collection_for_select(@parent_projects,
        'name', 'name',
        Project.find_by_identifier(params[:parent_id]).name),
    :include_blank => 'Select Project')

options_from_collection_for_select使您的选项列表,并接受一个被选中的参数。

于 2013-08-20T09:06:42.640 回答