2011-02-03 23 views
15

在我的控制器破壞作用,我想這個項目刪除後重定向到索引,我想傳遞一個變量叫「檢查」時重定向:如何通過redirect_to傳遞一個變量?

def destroy 
    @Car = Car.find(params[:id]) 
    checked = params[:checked] 

    if @car.delete != nil 

    end 

    redirect_to cars_path #I would like to pass "checked" with cars_path URL (call index) 
end 

如何通過這種「檢查」變量用cars_path這樣在我的索引函數中我可以得到它? (cars_path調用指數功能)

def index 
checked = params[checked] 
end 
+0

看看這個:http://stackoverflow.com/questions/1430247/passing-parameters-in-rails-redirect -至 – kafuchau 2011-02-03 14:29:24

回答

46

如果你不介意的PARAMS在URL中顯示,你可以:

redirect_to cars_path(:checked => params[:checked]) 

如果你真的介意的話,你可以通過傳遞會話變量:

def destroy 
    session[:tmp_checked] = params[:checked] 
    redirect_to cars_path 
end 

def index 
    checked = session[:tmp_checked] 
    session[:tmp_checked] = nil # THIS IS IMPORTANT. Without this, you still get the last checked value when the user come to the index action directly. 
end