Forum: Ruby on Rails passing params between controller methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ca93b7c135564e9eecba37780c0458c5?d=identicon&s=25 glenn (Guest)
on 2007-02-12 03:50
(Received via mailing list)
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
77f306388bb6ae00ac0b0401e27cdc99?d=identicon&s=25 Chad Fowler (Guest)
on 2007-02-12 06:00
(Received via mailing list)
On 2/11/07, glenn <glenn@tangelosoftware.net> 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
Ca93b7c135564e9eecba37780c0458c5?d=identicon&s=25 glenn (Guest)
on 2007-02-12 12:14
(Received via mailing list)
> 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
This topic is locked and can not be replied to.