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