2015-06-19 71 views
0

我試圖在我的Rails應用程序中實現動態選擇菜單,遵循YouTube視頻#88。Rails中的動態選擇菜單

我設置了一切像視頻,但我的JavaScript不工作,因爲它應該。

我的視圖代碼:_form.html.erb

<div class="field"> 
    <%= f.label :cliente_id %><br> 
    <%= f.collection_select :cliente_id, Cliente.order(:name), :id, :name, include_blank: true %> 
</div> 
<div class="field"> 
    <%= f.label :projeto_id %><br> 
    <%= f.grouped_collection_select :projeto_id, Cliente.order(:name), :projetos, :name, :id, :name, include_blank: true %> 
</div> 

relatos.coffe - >

JQuery -> 
    projetos = $('#relato_projeto_id').html() 
    $('#relato_cliente_id').change -> 
    cliente = $('#relato_cliente_id :selected').text() 
    options = $(projetos).filter("optgroup[label='#{cliente}']").html() 
    if options 
     $('#relato_projeto_id').html(options) 
    else 
     $('#relato_projeto_id').empty() 

該網頁似乎正常,沒有錯誤,但是當我列出了Cliente(客戶端)它應該列出只列出給他的項目,但它一直顯示所有項目,因爲根本沒有jQuery。

+1

請確認js文件的擴展名是「.coffee」而不是「.coffe」 –

+0

發佈生成的HTML,與rails相反,因爲它沒有幫助。 – lshettyl

+0

CoffeeScript是縮進敏感的。你所提供的不是語法上有效的。 –

回答

0

代碼的開始是JQuery,它應該是jQuery。感謝之前說過的人,但現在我明白了。