【问题标题】:how to pass a variable with redirect_to?如何使用redirect_to 传递变量?
【发布时间】:2011-02-03 14:25:26
【问题描述】:

在我的控制器销毁函数中,我想在删除项目后重定向到索引,并且我想在重定向时传递一个名为'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 传递这个“已检查”变量,以便在我的索引函数中得到它? (cars_path 调用 index 函数)

def index
 checked = params[checked]
end

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 2020-12-20
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2013-12-08
    相关资源
    最近更新 更多