带有嵌套资源的Rails 3.2 jQuery可排序列表500错误 [英] Rails 3.2 jQuery sortable list 500 error with nested resources

查看:63
本文介绍了带有嵌套资源的Rails 3.2 jQuery可排序列表500错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循有关可排序列表的Railscasts情节( http://railscasts. com/episodes/147-sortable-lists-revised?view = asciicast ),我已经准备好了所有内容,但是它不是对#show操作显示列表的模型进行排序,而是嵌套模型

Following the Railscasts episode about sortable lists (http://railscasts.com/episodes/147-sortable-lists-revised?view=asciicast), I've got everything in place, however instead of doing sorting on the model whose #show action is displaying the list, it's a nested model.

我的网站用于管理非常复杂的幻想足球联赛.联盟的一个方面是,通用汽车有年薪上限,球员有价值,球员可以签订多年合同.

My site is for managing a very complex fantasy football league. One of the aspects of the league is that GMs have a yearly salary cap amount, players have a value, and players can be signed to multi-year contracts.

无论如何,我正在创建一份草稿名册列表,使您可以添加球员并跟踪想要尝试的球员.我希望此列表是可排序的.我使用has_many:through关系进行设置.

Anyway, I'm creating a draft roster list that lets you add players and keep track of the ones you want to try and draft. I want this list to be sortable. I have it setup using a has_many :through relationship.

这是三个模型:

class DraftRoster < ActiveRecord::Base
  attr_accessible :name, :team_id

  belongs_to :team

  has_many :roster_spots
  has_many :players, through: :roster_spots
  ...
end

然后:

class RosterSpot < ActiveRecord::Base
  attr_accessible :draft_roster_id, :player_id, :position
  belongs_to :draft_roster
  belongs_to :player

  acts_as_list
end

最后(由于此处不适用于此问题,此处删除了很多代码):

And finally (this has a LOT more code removed here as its not applicable to this issue):

class Player < ActiveRecord::Base
  attr_accessible :auction_value, :first_name, :last_name, :nfl_team, :position, :is_drafted, :is_bought_out, :is_extended, :is_franchised, :bye_week, :full_name, :contracts_attributes, :subcontracts_attributes
  ...
  has_many :roster_spots
  has_many :draft_rosters, through: :roster_spots
  ...
end

因此,我经历了整个过程,并且我完全遵循了Railscasts一集.我的#sort动作在roster_spots_controller.rb文件中(位置属性也在此模型上).

So I've gone through and I've follow the Railscasts episode exactly. My #sort action is in the roster_spots_controller.rb file (and the position attribute is also on this model).

draft_rosters_controller.rb

draft_rosters_controller.rb

def sort
  params[:roster_spot].each_with_index do |id, index|
    RosterSpot.update_all({ position: index + 1 }, { id: id })
  end
  render nothing: true
end

我有必要的路线:

resources :roster_spots do
  collection { post :sort }
end

咖啡脚本:

jQuery ->
  $('#draft-roster-players').sortable(
    axis: 'y'
    handle: '.handle'
    update: ->
      $.post($(this).data('update-url'), $(this).sortable('serialize'))
)

draft_rosters#show操作:

The draft_rosters#show action:

def show
  @draft_roster = DraftRoster.find(params[:id])
  @team = @draft_roster.team
  @players = @draft_roster.players.order("position")

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @draft_roster }
  end
end

最后,视图:

<ul id="draft-roster-players" data-update-url="<%= sort_roster_spots_url %>">
  <% @players.each do |player| %>
    <%= content_tag_for :li, player do %>
        <span class="handle">[drag]</span>
        <%= link_to h(player.full_name), player %>
    <% end %>
  <% end %>
</ul>

一切都在按预期方式进行拖放,但是当我将播放器拖放到列表中的新位置时,在Chrome JavaScript控制台中,我不断收到此错误:

Everything is dragging and dropping as expected, but when I drop a player into a new spot on the list, in the Chrome JavaScript console, I keep getting this error:

 POST http://localhost:3000/roster_spots/sort 500 (Internal Server Error)
   send
   x.extend.ajax
   x.(anonymous function)
   $.sortable.update
   $.Widget._trigger
   $.widget._trigger
   (anonymous function)
   (anonymous function)
   $.widget._clear
   (anonymous function)
   $.widget._mouseStop
   (anonymous function)
   $.widget._mouseUp
   (anonymous function)
   _mouseUpDelegate
   x.event.dispatch
   v.handle

我不明白.我唯一能想到的就是它与嵌套资源有某种关系,但是我不明白为什么这一定很重要.关于Railscasts一集的一些评论暗示嵌套资源存在一些问题,但没有一个提供了使它正常工作的解决方案,而这是几年前的事了.

I don't get it. The only thing I can think of is that it somehow has to do with the nested resources, but I don't see why that would matter necessarily. A few comments on the Railscasts episode alluded to having some issues with nested resources, but none of them provided the solution they used to get it working and they're from years ago.

有人在Rails 3.2应用程序中通过jQuery完成可排序列表吗?使它起作用的必要诀窍是什么?我尝试将一些变量传递给标记上的data-update-url属性,但没有任何效果.

Has anyone done sortable lists through jQuery in a Rails 3.2 app? What's the trick necessary to get it working? I've tried passing a few variables to the data-update-url attribute on the tag but nothing has worked.

任何帮助将不胜感激!

UPDATE 愚蠢的是,我从未检查过Rails控制台的输出...我收到此错误:

UPDATE Stupidly, I never checked the Rails console output...I'm getting this error:

NoMethodError (undefined method `each_with_index' for nil:NilClass):
  app/controllers/roster_spots_controller.rb:89:in `sort'

推荐答案

通过控制台输出了解它!我必须进行以下更改:

Figured it out looking at the console output! I had to make the following changes:

draft_rosters#show视图:

draft_rosters#show view:

<ul id="draft-roster-players" data-update-url="<%= sort_roster_spots_url %>">
<% @roster_spots.each do |roster_spot| %>
    <%= content_tag_for :li, roster_spot do %>
        <span class="handle">[drag]</span>
        <%= link_to h(roster_spot.player.full_name), roster_spot %>
    <% end %>
<% end %>
</ul>

draft_rosters#show动作:

draft_rosters#show action:

def show
  @draft_roster = DraftRoster.find(params[:id])
  @team = @draft_roster.team
  @roster_spots = @draft_roster.roster_spots.order("position")

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @draft_roster }
  end
end

roster_spots_controller.rb

roster_spots_controller.rb

def sort
  params[:roster_spot].each_with_index do |id, index|
    RosterSpot.update_all({ position: index + 1 }, { id: id })
  end
  render nothing: true
end

现在它可以正常工作了. :)我希望这对其他人有帮助,如果他们发现自己处于这种情况下.

And now it's working as it should be. :) I hope that this can be of help to others should they find themselves in this situation.

这篇关于带有嵌套资源的Rails 3.2 jQuery可排序列表500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆