0

lengthJavascript で未定義のエラーが発生しています。

変更時に 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()の変更は機能しますが、選択ボックスから別のオプションを選択すると、lengthjquery エラーが発生します。

正確には:

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リクエストは停止しています。何か案は?ありがとう。

4

1 に答える 1

0

obj関数に を送信していないため、この jQuery を受信して​​いますchange()選択したオプション値を AJAX のアクションに送信してみてください。

# workout.js.coffee
$(".team-selection-workouts").change ->
    $.post "/workouts/filter_by_team",
    team_id: $(".team-selection-workouts :selected").val() # notice the `:selected` pseudo-selector
于 2013-09-27T15:49:44.820 に答える