ruby-on-rails – 如何使用redirect_to传递变量?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何使用redirect_to传递变量?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的控制器破坏功能中,我想重定向到索引后,项目删除,我想传递一个变量,称为“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

猜你在找的Ruby相关文章