ruby-on-rails – 如何使用redirect_to传递变量?
发布时间:2020-12-16 20:41:34 所属栏目:百科 来源:网络整理
导读:在我的控制器破坏功能中,我想重定向到索引后,项目删除,我想传递一个变量,称为“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 "checke
在我的控制器破坏功能中,我想重定向到索引后,项目删除,我想传递一个变量,称为“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 如何通过这个’checked’变量与cars_path,以便在我的索引函数我可以得到它? (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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |