如何使用redirect_to传递变量?

在我的控制器销毁function中,我希望在项目删除后重定向到索引,并且我想在重定向时传递一个名为’checked’的变量:

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传递这个’checked’变量,以便在我的索引函数中我能得到它? ( cars_path调用索引函数)

 def index checked = params[checked] end 

如果您不介意在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