Ruby-on-rails rails 更改控制器中的参数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5014804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:26:41  来源:igfitidea点击:

rails change value of parameter in controller

ruby-on-rails

提问by rahardi

I have this controller

我有这个控制器

 def mymethod
  @theparam => (params[:valueoftheparam])
  @theparam => "3"
  callothermethodthatusetheparam
 end

So basically, I have "valueoftheparam" which is "2".
I need to change the value of "2" into "3", and let "callothermethodthatusetheparam" the new param (which is "3")
however, "callothermethodthatusetheparam" in the end still used the old value("2").

所以基本上,我有“valueoftheparam”,即“2”。
我需要将“2”的值更改为“3”,并让“callothermethodthatusetheparam”使用新参数(即“3”),
但是,“callothermethodthatusetheparam”最终仍使用旧值(“2”)。

How I can change this value in the controller, and let "callothermethodthatusetheparam" to use the new param value?

如何在控制器中更改此值,并让“callothermethodthatusetheparam”使用新的参数值?

Thank you!

谢谢!

回答by Fernando Diaz Garrido

You have to modify the value directly, the instance variable does not point to the param, it just clones its value

你必须直接修改值,实例变量不指向参数,它只是克隆它的值

params[:valueoftheparam] = 3

回答by rubyprince

If you do like this, I am sure you will get 3 printed (@params will be "3")

如果你这样做,我相信你会得到 3 个打印(@params 将是“3”)

def my_method
  @param = (params[:valueoftheparam])
  @param = "3"
  other_method
end


def other_method
  puts @param
end