0

jQuery-UI の並べ替え可能な接続リストの位置を Rails バックエンドに保存しようとしています。パラメータをデータベースに保存しようとすると、Rails Controller アクションに問題が発生します。私が得ているエラーはTypeError (can't convert Symbol into Integer):

私が送信している Ajax POST リクエストのパラメーターは次のようになります。

Activity:[{"id":"65","column":"48"},{"id":"65","column":"48"},{"id":"67","column":"48"}]

私の Rails コントローラ アクションは次のとおりです (「id」の ID でアクティビティを更新しようとしており、属性 position を「position」で更新し、day_id を「column」で更新しようとしています:

  def sort
    JSON.parse(params[:Activity]).each_with_index do |x, index|
      id = x["id"]
      position = index+1
      column = x["column"]
      Activity.update_all(position = position, day_id = column, id = id)
    end
    render nothing: true
  end

ターミナルで次のエラーが表示されます。

    Started POST "/trips/sort" for 10.0.2.2 at 2012-11-04 23:52:30 +0000
Processing by TripsController#sort as */*
  Parameters: {"Activity"=>"[{\"id\":\"66\",\"column\":\"49\"},{\"id\":\"66\",\"column\":\"49\"},{\"id\":\"67\",\"column\":\"49\"}]"}
Completed 500 Internal Server Error in 1ms

TypeError (can't convert Symbol into Integer):

参考になる場合、これは私の jQuery AJAX 呼び出しです。

    jQuery ->
  $('[id*="day"]').sortable(
    connectWith: ".day"
    placeholder: "ui-state-highlight"
    update: (event, ui) ->
      neworder = new Array()
      $(this).children().each ->
        column = $(this).parent().attr("id").match(/\d+/)[0]
        id = $(this).attr("id").match(/\d+/)[0]
        neworder.push(
          id: id
          column: column
        )
      alert neworder
      $.ajax
        url: "sort"
        type: "POST"
        data: { Activity: JSON.stringify(neworder) }
    ).disableSelection()

私はこれに過去数時間を費やしましたが、それを理解することはできません. 時間と助けに本当に感謝しています。

4

1 に答える 1

1

次のように、各アクティビティを個別に更新します。

JSON.parse(params[:Activity]).each_with_index do |x, index|
  id = x["id"]
  position = index+1
  column = x["column"]
  activity = Activity.find(id)
  activity.update_attributes(:position=>position, :column=>column)
end

(JSON解析が正しく機能すると仮定しますが、私はチェックしていません)

Activity.update_allすべてのアクティビティを同じ属性で更新します (たとえば、すべてのアクティビティを列 5 と位置 1 に設定します)。これは、ここで達成したいことではありません。

于 2012-11-05T03:29:47.437 に答える