Ruby-on-rails 如何使用redirect_to传递变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4887321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass a variable with redirect_to?
提问by Mellon
In my controller destroy function, I would like to redirect to index after the item deleted, and I would like to pass a variable called 'checked' when redirect:
在我的控制器销毁函数中,我想在删除项目后重定向到索引,并且我想在重定向时传递一个名为“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
how to pass this 'checked' variable with cars_pathso that in my index function I can get it?? (cars_pathcalls indexfunction)
如何使用cars_path传递这个'checked'变量,以便在我的索引函数中我可以得到它??(cars_path调用索引函数)
def index
checked = params[checked]
end
回答by PeterWong
If you do not mind the params to be shown in the url, you could:
如果您不介意在 url 中显示参数,您可以:
redirect_to cars_path(:checked => params[:checked])
If you really mind, you could pass by session variable:
如果你真的介意,你可以通过会话变量:
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

