I have this simple rails code and something weird is happening. As you can see params.nil? is true and it's still calling the else part. What am I missing here?
Pry Session
5: def build_resource
6: binding.pry
7: if params.nil?
8: model_class.new
9: else
=> 10: params = params.merge(dealer: {})
11: model_class.new(dealer_params)
12: end
13: end
[3] pry(#<Admin::DealersController>):1> params.nil?
true
No, Its not a pry issue. You just cant reassign params to params. Try using different variable. This should work fine.
You can use as
Digging Deeper inside Rails. Have a look at this.
Simple Explanation: params are responsible for carrying value which you permit inside your controller, but its resigning it will return false or nil. You can study about these topics in dept you get to know.
https://apidock.com/rails/Class/cattr_accessor
That's why when you declare new variable the value of params => (object) is assigns to it. But when you do the same thing with params or method that return an object this will give nil.
SAME Answer: you just can't reassign params to params.