![](/img/trans.png)
[英]Alternative ways to pass model from one controller action to another in RAILS
[英]Rails: How to pass value from one action to another in the same controller
我從第一個動作中獲得輸入,如何將值傳遞給另一個動作?
example_controller.rb
def first
@first_value = params[:f_name]
end
def second
@get_value = @first_value
end
您不能傳遞實例變量,因為第二個操作正在控制器的新實例中運行。 但是你可以傳遞參數...
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
變量,這通常是您為用戶存儲值的方式......不要只存儲整個對象,因為會話存儲通常是有限的
def first
@first_value = params[:f_name]
session[:passed_variable] = @first_value
end
def second
@first_value = session[:passed_variable]
@get_value = @first_value
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.