我想通過Javascript或Ruby on Rails代碼對新位置進行排序後重新加載頁面。如何使用JavaScript或Ruby on rails進行頁面重新加載
$("#serialize").click ->
c = set: JSON.stringify($("#sortable").nestedSortable("toHierarchy",
startDepthCount: 0
))
$.post "savesort", c, $("#output").html("<p id=\"flash_notice\">Saved Successfully</p>")
false
我想在這裏將它添加
$.post "savesort", c, $("#output").html("<p id=\"flash_notice\">Saved Successfully</p>")
window.location.reload(false);
false
,但好像是攪亂秩序。這裏是我的Rails代碼
class SiteController < ApplicationController
def savesort
neworder = JSON.parse(params[:set])
prev_item = nil
neworder.each do |item|
dbitem = Category.find(item['id'])
prev_item.nil? ? dbitem.move_to_root : dbitem.move_to_right_of(prev_item)
sort_children(item, dbitem) unless item['children'].nil?
prev_item = dbitem
end
Category.rebuild!
render :nothing => true
end
end
我也想改變渲染:沒什麼=>真實redirect_to的root_url但似乎沒有任何工作。
這裏是我的routes.rb(縮短了空間的緣故)
locksmithing::Application.routes.draw do
get "site/home"
match "/savesort" => 'site#savesort'
root to: 'site#home'
end
所以,我應該在哪裏添加代碼來刷新頁面? Javascript或在網站控制器?還是有另一種解決方案?提前致謝。
這是一些很好的信息,我能夠按照你的解決方案,它的工作。非常感謝,我是Javascript/Jquery/Coffeescript的新手,所以當我將這個從javascript轉換爲coffeescript時必定發生。我知道它的工作原理,並會嘗試你提到的AJAX方法。 – ruevaughn 2012-02-29 19:22:51