在服务器端给 ActionController::Parameters 添加一个值 [英] Adding a value to ActionController::Parameters on the server side
本文介绍了在服务器端给 ActionController::Parameters 添加一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将我在服务器上评估的一些值添加到参数中.
I want to add some value I'm evaluating on the server to the params.
def my_params
params.require(:my_model).permit(:my_field1, :my_field2, :my_field3)
end
def update
# ..................
# checking
my_params["my_value"].class # => ActionController::Parameters
my_params["my_value"] # => nil
# adding
my_params["my_value"] = "my value's value" # => "my value's value"
# but it's still nil
my_params["my_value"] # => nil
# ..................
end
为什么我不能将其添加到 my_params
中?
Why can't I add it into my_params
?
推荐答案
my_params
是方法不是变量,使用实例变量代替,如下
my_params
is a method not a variable, use an instance variable instead, as below
def my_params
@my_params = params.require(:my_model).permit(:my_field1, :my_field2, :my_field3)
end
def update
@my_params["my_val"] = "my value"
#....
end
这篇关于在服务器端给 ActionController::Parameters 添加一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文