Alder Green
2006-Aug-29 17:27 UTC
How to pass params from an action to another you redirect_to
Suppose action foo #redirects_to action bar. Once #redirected_to, bar would not have action foo''s params. One simple solution is: params[:action] = ''bar'' redirect_to(params) With that params is preserved in bar. The problem is that the HTTP method of bar is GET, so the params are shown in the URL. Any better solution? More broadly, any other way to pass data between foo and bar, besides sticking it in session? -Alder -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2006-Aug-29 17:55 UTC
Re: How to pass params from an action to another you redirect_to
That''s one of the chief reasons for the session.. to persist parameters between requests without using the URL. I used this method all the time in my old ASP applications. At least Rails makes it easy On 8/29/06, Alder Green <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Suppose action foo #redirects_to action bar. Once #redirected_to, bar > would not have action foo''s params. > > One simple solution is: > > params[:action] = ''bar'' > redirect_to(params) > > With that params is preserved in bar. The problem is that the HTTP > method of bar is GET, so the params are shown in the URL. > > Any better solution? > > More broadly, any other way to pass data between foo and bar, besides > sticking it in session? > > -Alder > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jeffrey Hardy
2006-Aug-29 18:02 UTC
Re: How to pass params from an action to another you redirect_to
On 29-Aug-06, at 1:27 PM, Alder Green wrote:> More broadly, any other way to pass data between foo and bar, besides > sticking it in session?I think the session is your only bet. Why not store the params in the flash? That way you won''t have to worry about manually expiring it like you would if you created your own session. flash[:params] = params /Jeff -- http://re.visioni.st http://quotedprintable.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Alder Green
2006-Aug-29 18:32 UTC
Re: How to pass params from an action to another you redirect_to
Brian Hogan wrote:> That''s one of the chief reasons for the session.. to persist parameters > between requests without using the URL. I used this method all the time in > my old ASP applications. At least Rails makes it easyOk, but what would you do in the following common scenario: #bar requires two parameters, bart and lisa. Normally, #bar is invoked directly, by a POST request supplying it with those parameters. But sometimes, you want to #redirect_to it from some other method, say #foo. One option is to write a mechanism at the beginning of the method which abstracts bart and lisa (e.g. assigns them to local variables) so that the rest of the method is indifferent as to whether bart and lisa were supplied by a direct POST or by passed in the session. This is awkward and the result will be somewhat bloated imho. Another option is to transfer all of #bar''s functionality to a new method, #baz, that is ALWAYS invoked indirectly, by a #redirect_to, so it ALWAYS looks for bart and lisa in the session. Then of course you make the original #bar a thin wrapper that takes bart and lisa from params, sticks them in session where #baz would find them, and redirects_to #baz. This latter one is more elegant, but I wonder if all those redirects are really a Good Idea, considering performance issues and such. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2006-Aug-29 19:30 UTC
Re: How to pass params from an action to another you redirect_to
Use a before filter to pull the data into an instance variable from the
session if the session has the key. Then invoke this filter on any methods
that have multiple entrypoints.
before_filter :get_parameter_data, :only =>[:create, :update]
def create
@bar = Bar.new(@p[:bar])
@if bar.save
....
end
def update
end
private
# could even make this alter params[] directly if you really wanted to... no
reason why not
def get_parameter_data
if session[:params].nil?
@p = params
else
@p = session[:params]
session[:params] = nil
end
end
How does that work foyou? Or am I not quite understanding this?
On 8/29/06, Alder Green
<alder.green-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
>
>
> Brian Hogan wrote:
> > That''s one of the chief reasons for the session.. to persist
parameters
> > between requests without using the URL. I used this method all the
time
> in
> > my old ASP applications. At least Rails makes it easy
>
> Ok, but what would you do in the following common scenario:
>
> #bar requires two parameters, bart and lisa. Normally, #bar is invoked
> directly, by a POST request supplying it with those parameters. But
> sometimes, you want to #redirect_to it from some other method, say
> #foo.
>
> One option is to write a mechanism at the beginning of the method which
> abstracts bart and lisa (e.g. assigns them to local variables) so that
> the rest of the method is indifferent as to whether bart and lisa were
> supplied by a direct POST or by passed in the session.
>
> This is awkward and the result will be somewhat bloated imho.
>
> Another option is to transfer all of #bar''s functionality to a new
> method, #baz, that is ALWAYS invoked indirectly, by a #redirect_to, so
> it ALWAYS looks for bart and lisa in the session. Then of course you
> make the original #bar a thin wrapper that takes bart and lisa from
> params, sticks them in session where #baz would find them, and
> redirects_to #baz.
>
> This latter one is more elegant, but I wonder if all those redirects
> are really a Good Idea, considering performance issues and such.
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---