我正在尝试实现基于Railscast#88的动态选择,但无济于事。我上下搜索但没有成功。
我有Staff
,Question
和Redesignation
型号。
class Staff < ApplicationRecord
belongs_to :redesignation
belongs_to :question
end
class Question < ApplicationRecord
has_many :redesignations
has_many :staffs
end
class Redesignation < ApplicationRecord
has_many :staffs
belongs_to :question
end
在创建新员工的_form部分中,当选择一个问题时,将相应地填充重新指定。
<%= f.label :question_id,'Do you feel you are in the right designation?' %>
<%= f.collection_select :question_id, Question.order(:name), :id, :name,
include_blank: true %>
<%= f.label :redesignation, 'Proposed Redesignation' %>
<%= f.grouped_collection_select :redesignation_id, Question.order(:name),
:redesignations, :name, :id, :redesignation, include_blank: true %>
这是staffs.coffee中的jQuery:
jQuery ->
$('#staff_redesignation_id').parent().hide()
redesignations = $('#staff_redesignation_id').html()
$('#staff_question_id').change ->
question = $('#staff_question_id :selected').text()
escaped_question = question.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g,
'\\$1')
options = $(redesignations).filter("optgroup[label='#
{escaped_question}']").html()
if options
$('#staff_redesignation_id').html(options)
$('#staff_redesignation_id').parent().show()
else
$('#staff_redesignation_id').empty()
$('#staff_redesignation_id').parent().hide()
尽管我付出了最大努力,但我还没有看到光明。我可能会出错的任何想法?
编辑:这是我缩进的图片:enter image description here