我要提出一个非常棒的问题!
railscasts.com/episodes/88-dynamic-select-menus-revised 这一集的应用非常简单,但我需要在我的应用程序中使用动态菜单,这有点像#34;更长的时间。 在剧集中使用了一个文件people.js.coffe,该文件在应用创建新人时运行
在我的应用程序中,当我创建一个新modulo1时,可以调用此文件(在localhost:3000 / users / 1 / modulo1s / new)
问题是:我必须在哪里写coffe脚本文本?在什么档案? (application.js.coffe,modulo1s.js.coffe ...)我试着把它放在任何地方,但它没有用!
application.js
jQuery ->
$('#categorie_id').parent().hide()
categories = $('#categorie_id').html()
$('#_macrocategorie_id').change ->
macrocategorie = $('#macrocategorie_id :selected').text()
options = $(categories).filter("optgroup[label='#{macrocategorie}']").html()
if options
$('#categorie_id').html(options)
$('#categorie_id').parent().show()
else
$('#categorie_id').empty()
$('#categorie_id').parent().hide()
形式
<div class="form-field">
<%= f.label :macrocategorie_id %><br>
<%= f.collection_select :macrocategorie_id, Macrocategorie.order(:nome), :id,
:nome, {:prompt => "Seleziona una Macrocategoria"} %>
<%= f.label :categorie_id %>
<%= f.grouped_collection_select :categorie_id,
Macrocategorie.order(:nome),:categories,:nome, :id,:nome,
{:prompt => "Seleziona una Categoria"} %>
</div>
如果它可能有用
class Macrocategorie < ActiveRecord::Base
has_many :categories,class_name: 'Categorie'
has_many :modulo1s
accepts_nested_attributes_for :categories
attr_accessible :nome, :categories_attributes
end
class Categorie < ActiveRecord::Base
belongs_to :macrocategorie,class_name: 'Macrocategorie'
has_many :modulo1s
attr_accessible :nome, :macrocategorie_id, :microcategories_attributes
end