无法在f.select中将符号转换为整数

时间:2014-03-14 07:12:32

标签: ruby-on-rails

在搜索页面中,我有一个下拉列表,即从students表填充。当我从下拉列表中选择一个值并在点击搜索按钮后,下拉列表的选定值将丢失,下面是下拉列表的代码:

<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
    <table>
     <tr>
        <td align="center">
           <%= f.select(:Interest,options_from_collection_for_select(@students, "id", "student_mentor_subjects"), {},:id => "DDL_Students", :style => "width:160px;") %>
         </td>
     </tr>
     <tr>
        <td>
           <div class="button">
               <input type="submit" name="search" value="Search" class="buttonSearch">
           </div>
        </td>
     </tr>   
    </table>
<% end %>

然后在谷歌之后,我将此params[:search][:Interest] || @students.id放入我的代码

<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
        <table>
         <tr>
            <td align="center">
               <%= f.select(:Interest,options_from_collection_for_select(@students, "id", "student_mentor_subjects", params[:search][:Interest] || @students.id), {},:id => "DDL_Students", :style => "width:160px;") %>
             </td>
         </tr>
         <tr>
            <td>
               <div class="button">
                   <input type="submit" name="search" value="Search" class="buttonSearch">
               </div>
            </td>
         </tr>   
        </table>
    <% end %>

但是通过使用上面的代码,我收到以下错误:

can't convert Symbol into Integer
下面是&#34;学生&#34;的模式。表:

id | student_mentor_subjects
1  | abc
2  | def
3  | ijk

我该如何解决这个问题。请建议我。感谢

1 个答案:

答案 0 :(得分:0)

您希望能够发布搜索并保留您之前选择的值吗?

试试这个:

<%= f.select(:Interest,options_from_collection_for_select(@students, "id", "student_mentor_subjects"), {}, :selected => params[:search], :id => "DDL_Students", :style => "width:160px;") %>

<%= f.select(:Interest,options_from_collection_for_select(@students, "id", "student_mentor_subjects"), {}, :selected => params[:search][:Interest], :id => "DDL_Students", :style => "width:160px;") %>

第二个是完整的猜测,