length
Javascript で未定義のエラーが発生しています。
変更時に ajaxpost
関数がありますselect tag
index.html.erb:
<%= select_tag :team_id, options_from_collection_for_select(Team.where(campus_id:current_user.campus_id).order('name'), 'id', 'name'), class:"team-selection-workouts", prompt:"Select" %>
ワークアウト.js.coffee の関数を変更します。
$(".team-selection-workouts").change ->
$.post "/workouts/filter_by_team",
team_id: $(".team-selection-workouts").val()
ルート.rb
resources :workouts do
collection do
post :filter_by_team
end
end
コントローラーのアクションworkouts_controller.rb
そこでteam_id
、上記の coffeescript から ajax を使用して値を渡しました。
def filter_by_team
# For Trainers
@team = Team.find(params[:team_id])
@workouts = Workout.find(:all, order: 'name', :conditions => ['team_id = ? and status = ?', @team.id, 'new'])
end
私のfilter_by_team.js.erbでは:
@workouts
パーシャルのオブジェクトを渡しました
$('#team-workouts').html("<%=j render :partial => 'workouts/filtered_by_team', locals: {workouts: @workouts}%>");
すべての選択ボックスchange()
は、パーシャルを次に従ってレンダリングする必要があります。team_id
_filtered_by_team.html.erb:
部分ファイルにローカルを渡すworkouts
ようになりました...複数選択ボックスがあります...
<%= select_tag :workout, options_from_collection_for_select(workouts, "id", "name"), size: 7, class: 'excercises_muliti_select' %>
最初select()
の変更は機能しますが、選択ボックスから別のオプションを選択すると、length
jquery エラーが発生します。
正確には:
Uncaught TypeError: Cannot read property 'length' of undefined jquery.js?body=1:606
jQuery.extend.each jquery.js?body=1:606
(anonymous function) jqBootstrapValidation.js?body=1:459
jQuery.extend.each jquery.js?body=1:632
jQuery.fn.jQuery.each jquery.js?body=1:254
(anonymous function) jqBootstrapValidation.js?body=1:457
jQuery.event.dispatch jquery.js?body=1:3046
elemData.handle
したがって、このため、post
リクエストは停止しています。何か案は?ありがとう。