gpt4 book ai didi

ruby-on-rails - rails : How to pass value from one action to another in the same controller

转载 作者:行者123 更新时间:2023-12-03 16:20:46 24 4
gpt4 key购买 nike

我从第一个 Action 获得输入,如何将值传递给另一个 Action ?

example_controller.rb

def first
@first_value = params[:f_name]
end

def second
@get_value = @first_value
end

最佳答案

您不能传递实例变量,因为第二个操作正在 Controller 的新实例中运行。但是你可以传递参数...

def first
@first_value = params[:f_name]
redirect_to second_action_path(passed_parameter: params[:f_name])
end

def second
@first_value = params[:passed_parameter]
@get_value = @first_value
end

您也可以使用 session变量,这是您通常为用户存储值的方式......不要仅存储整个对象,因为 session 存储通常是有限的
def first
@first_value = params[:f_name]
session[:passed_variable] = @first_value
end

def second
@first_value = session[:passed_variable]
@get_value = @first_value

关于ruby-on-rails - rails : How to pass value from one action to another in the same controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40227933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com