[英]What is the best way to give a model access to params hash in 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.