1
在我的Rails应用程序中,我试图构建一个可排序的列表(在RailsCast tutorial之后)。我有一个屏幕,该屏幕has_many
内容,它们嵌套:jQuery使用嵌套资源排序
screen.rb:
class Screen < ActiveRecord::Base
has_many :contents
end
content.rb:
class Content < ActiveRecord::Base
belongs_to :screen
end
的routes.rb:
resources :screens do
resources :contents do
collection { post :sort }
end
end
在我对屏幕的看法中,我想显示可排序内容的列表。
/screens/show.html.erb:
<ul id="content" data-update-url="<%= sort_screen_contents_path(@screen) %>">
<% @screen.contents.each do |content| %>
<li><%= content.name %></li>
<% end %>
</ul>
和相关的CoffeeScript:
jQuery ->
$('#content').sortable(
axis: 'y'
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
);
当我拖动的内容,我的服务器日志显示它经过screen_id作为参数,而不是内容位置:
Started POST "/screens/2/contents/sort" for 127.0.0.1 at 2013-11-25 12:39:21 -0600
Processing by ContentsController#sort as */*
Parameters: {"screen_id"=>"2"}
Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.3ms | ActiveRecord: 0.0ms)
我认为这个问题可能是我在哪里定义data-update-url
在视图中,因为我传递了@screen
,但它似乎需要当你有一个嵌套的资源。我应该如何调用Content#sort
操作?问题在别的地方吗?
谢谢,我给了一个拍摄前,但@ screen.contents似乎是一个数组。我呈现的HTML显示:'data-update-url =“/ screens/2 /%23%3CActiveRecord :: Associations :: CollectionProxy :: ActiveRecord_Associations_CollectionProxy_Content:0x007faf6a666180%3E/contents/sort”'我得到一个类似的路由错误那。 –
对不起,想到它,这不会起作用,我认为它在于你的html标记,因为js代码是序列化屏幕div而不是子div – TheIrishGuy