2014-03-14 61 views
-1

在搜索頁面中,我有一個下拉列表,即從students表填充。當我從下拉式和搜索按鈕點擊下拉列表中選擇的值丟失後的值,下面是下拉的代碼:無法將符號轉換爲整數f.select

<%= 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 
下面

是「學生」表的模式:

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

我該如何解決這個問題。請建議我。謝謝

回答

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;") %> 

第二個是完整的猜測,

+0

第一個我已經嘗試,但它不是給我的下拉列表中選擇的值成一個控制器頁面。第二個給我錯誤「不能將符號轉換爲整數」 – user88

相關問題