簡體   English   中英

在Rails中記住索引參數的最佳方法是什么?

[英]What's the best way to remember params for index in Rails?

通常,索引頁面具有用於過濾,頁面,排序的選項。 在顯示新的,編輯頁面之后,您可能想要返回與看到的選項相同的索引頁面。

為此,我將參數保存在會話中。

class ApplicationController < ActionController::Base

  helper_method :index_with_params
  after_filter :save_index_params, :only => [:index]

  def save_index_params 
    session[self.class.to_s] = params 
  end

  def index_with_params overrider={}
    object = self.kind_of?(ApplicationController) ? self : controller
    h = session[object.class.to_s] || {"action" => :index}
    h["only_path"] = true
    url_for(h.merge(overrider));
  end

end

我曾經使用ActiveRecord進行會話存儲。 現在我要使用cookie。 因此,會議應該很輕松。

您如何處理這種情況?

謝謝。

山姆

在顯示新的,編輯頁面之后,您可能想要返回與看到的選項相同的索引頁面。

如果您最初在查詢字符串中通過過濾選項,即。 example.com/widgets?order_by=price ,然后當您的用戶使用瀏覽器的“返回”按鈕導航回索引時,將保留過濾選項。

如果您確實希望在用戶以后在example.com/widgets鍵入內容時保留選項,那么您需要將這些選項保留在某個地方 ,通常在session對象中。 Cookie存儲區最多可容納4KB,因此除非您有很多選擇,否則應該會很好。 如果您需要更多空間或性能成為問題,請查看其他會話存儲,例如Redis Store。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM