Ruby on rails 如何将带有重定向的变量传递到?
在我的controller destroy函数中,我想在删除项后重定向到index,并且我想在重定向时传递一个名为“checked”的变量:Ruby on rails 如何将带有重定向的变量传递到?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,在我的controller destroy函数中,我想在删除项后重定向到index,并且我想在重定向时传递一个名为“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 car
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\u path传递这个“checked”变量,以便在我的索引函数中获得它??(cars\u path调用索引函数)
如果您不介意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
看看这个:
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