2011-02-15 4 views
5

사용자가 데이터베이스에서 개체의 하위 집합을 식별 할 수 있도록 일련의 3 개의 단추를 색인 페이지에 생성했습니다 - 단추 1 - type = "new" , 버튼 2 - 유형 = "사용", 버튼 3 - 제한 없음 즉, 새 것이거나 사용될 수 있습니다.컨트롤러의 페이지 간 매개 변수 값을 기억하는 방법

현재의 index.html.erb에는 다음이 포함 product.rb 내

<%= link_to "New", :controller => params[:controller], :action => params[:action], :product_type => 'New' %> 
<%= link_to "Used", :controller => params[:controller], :action => params[:action], :product_type => 'Used' %> 
<%= link_to "Don't Restrict", :controller => params[:controller], :action => params[:action], :product_type => nil %> 

는 내가 가지고 :

scope :by_product_type, lambda{|product_type| where(:product_type => product_type) unless product_type.nil? } 

마지막으로 내가 가진 productfinder_controller는 :

before_filter :load_grips, :only => [:index, :bybrand, :bycolour, :byprice] 
protected 
def load_products 
if params[:product_type] 
    @productssmatchingtype = Product.by_product_type(params[:product_type]) 
else 
    @productsmatchingtype = Product 
end 
end 

위의 코드는 정확히 작동 버튼 1 또는 버튼 2를 누르면 모든 항목을 처음으로로드하고 제한합니다.

또한 ProductFinder 컨트롤러의 다른 세 페이지, byprice, bybrand 및 bycolour에서도 비슷한 기능을 사용하고 싶습니다. 위의 3 개의 다른 .html.erb 파일에 위에 제공된 것과 동일한 코드를 넣었으며 다시 작동하는 것처럼 보입니다. ...

EXCEPT - 새 페이지가로드되면 기본값은 모든 제품이 즉, 사용자가 이전 페이지에서 어떤 버튼을 눌렀는지 기억하지 못했습니다. 컨트롤러의 모든 페이지에 액세스 할 수있는 어딘가에있는 버튼에 관한 정보를 저장할 수 있습니까? 비슷하게/또는 @productsmatchingtype (productfinder_controller에서)을 모든 페이지에 액세스 할 수 있고 처음부터 다시 시작할 필요가 없도록 정의 할 수 있습니까?

+1

세션에 상태를 저장하고 다음 요청시이를 확인할 수 있습니다. 세션 [: something] = "귀하의 가치" – Rafal

답변

7

전자 메일 요청에 대한 변수를 기억할 수 없습니다. 그러나 product_type을 세션에 저장할 수 있습니다.

def load_products 
    #get product_type from session if it is blank 
    params[:product_type] ||= session[:product_type] 
    #save product_type to session for future requests 
    session[:product_type] = params[:product_type] 
    if params[:product_type] 
    @productssmatchingtype = Product.by_product_type(params[:product_type]) 
    else 
    @productsmatchingtype = Product 
    end 
end 

그 작업을 수행해야합니다.

+0

두 분 모두 감사합니다 - 완벽하게 일했습니다! 이는 전 세계적으로 '더 많은'가치를 저장하는 방법과 관련하여 지난 몇 날을 찾고 있었기 때문에 매우 유용합니다. – Texas

관련 문제