Params

If there was some old code that, under certain conditions, would have changed the parameters. I believe that it worked before (and not 100%). Now we get the parameters equal to zero, regardless of whether the condition is satisfied.

The culprit is in this state, I am doing params = tmp.dup . Even when the condition is false, it causes an error in the update action.

I was able to recreate with a minimal test

(Rails 2.3.5)

 rails bug; cd bug; script/generate scaffold bug name:string; rake db:create; rake db:migrate; 

change applications / controllers / bugs_controller.rb add to start of update action

 l_p = params.dup if (false) params = l_p.dup # NOT REACHED end 

script / server WEBrick -p 5001

go to http: // localhost: 5001 / bugs create a new error fix the error submit

+4
source share
1 answer

For user comment 45147, the correct answer to this question is here:

assign / replace params hashes in rails

Copy here:

params , which contains the request parameters, is actually a method call that returns a hash containing the parameters. Your String params = assigns a local variable named params .

After the if false block, a local params variable appeared in Ruby, so when you refer to params later in the method, the local variable takes precedence over the method call with the same name. However, because your params = assignment is inside the if false block, the local variable is never assigned a value, so the local variable is nil .

If you try to access a local variable before its assignment, it will receive a NameError value:

 irb(main):001:0> baz NameError: undefined local variable or method `baz' for main:Object from (irb):1 

However, if there is a variable assignment that is not in the code execution path, then Ruby created a local variable, but its value is nil .

 irb(main):007:0> baz = "Example" if false => nil irb(main):008:0> baz => nil 
+5
source

All Articles