Passing params between controller methods


#1

Hi all

I’d like to call some controller methods from other controller
methods after first modifying the params hash.

Class MyController < ApplicationController
def quirky_method
if condtion
do_stuff
else
params.merge “some_key” => “here is some new data”
another_controller_method
end
end

the problem ive got is that the new data in the hash isn’t available
inside ‘another_controller_method’. i’ve also tried ‘self.params’ and
@params’, but doesnt seem to work -whats the best way to approach
this?
thx
glenn


#2

On 2/11/07, glenn removed_email_address@domain.invalid wrote:

 else
    params.merge "some_key" => "here is some new data"
    another_controller_method

end
end

the problem ive got is that the new data in the hash isn’t available
inside ‘another_controller_method’. i’ve also tried ‘self.params’ and
@params’, but doesnt seem to work -whats the best way to approach
this?

The reason the params aren’t updated is that #merge is not a
destructive method. It returns a new Hash which you’re just throwing
away (not referencing or assigning to a variable).

There is a destructive version, #merge!, which does what you want.
Just add the exclamation point, and you’ll get what you’re expecting.

But, I think what you should probably do is make
another_controller_method accept the parameters it’s expecting as
explicit arguments and call it like that. Munging params is bound to
lead to some messes later on.

Chad


#3

The reason the params aren’t updated is that #merge is not a
destructive method. It returns a new Hash which you’re just throwing
away (not referencing or assigning to a variable).

There is a destructive version, #merge!, which does what you want.
Just add the exclamation point, and you’ll get what you’re expecting.

ok - a vital detail - thanks chad - dangers of learning a language
along with a toolkit implimented in it

But, I think what you should probably do is make
another_controller_method accept the parameters it’s expecting as
explicit arguments and call it like that. Munging params is bound to
lead to some messes later on.
good advice - thanks, what i ended up doing in end

thanks again chad
glenn