My code is something like this. class FirstController < ApplicationController def foo #some processing render (:controller => ''second'', :action => ''index'') end end I tried using both render and redirect but it seems it''s not possible to send the control from within a controller to some other controller. Am I missing something here? Thanks. -=- Neeraj -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060114/d001e803/attachment.html
Are your redirect_to or render statements the last line in your controller method that is performing the redirect? Are you always redirecting, or doing it conditionally? redirect_to and render do not return a value, so your controller method will continue to run after calling them (if there are additional statements to process), unless you do something like the following: redirect_to :controller => ''controller'', :action => ''myAction'' and return I''m a relative rails newb, so I could be horribly misleading you, but I ran into a similar problem and the above suggestion took care of it... hopefully someone with more experience can weigh in here for a definitive answer. -Will On 1/14/06, Neeraj Kumar <neeraj.jsr@gmail.com> wrote:> > My code is something like this. > > class FirstController < ApplicationController > > def foo > #some processing > render (:controller => ''second'', :action => ''index'') > end > > end > > > I tried using both render and redirect but it seems it''s not possible to > send the control from within a controller to some other controller. Am I > missing something here? > > Thanks. > -=- Neeraj > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060114/f4c0e11f/attachment.html
Matthew Palmer
2006-Jan-14 20:47 UTC
[Rails] Re: How to render from a different controller
On Sat, Jan 14, 2006 at 03:08:15PM -0500, Neeraj Kumar wrote:> My code is something like this. > > class FirstController < ApplicationController > > def foo > #some processing > render (:controller => ''second'', :action => ''index'') > end > > end > > I tried using both render and redirect but it seems it''s not possible to > send the control from within a controller to some other controller. Am I > missing something here?the ''render'' method is for specifying a view template to render, so you could go with render ''second/index'' If *all* you want to do is use the view from another controller action. However, if you wanted to use all of the code of the other controller, you''d need to do something like this in your *view* for first/foo: <%= render_component :controller => ''second'', :action => ''index'' %> I''ve found this useful for putting intelligent sidebars in my views (where a partial just won''t cut it). It''s hard to make specific recommendations for your situation without knowing more about what you''re trying to do. What is this "#some processing" you''re doing? Why do you want to render another controller''s action? For instance, if you just want to go to the other action, a quick redirect_to :controller => ''second'', :action => ''index'' and return (as previously suggested) will do the trick. If ''#some processing'' is just (for example) handling a POST request and saving some data, then redirect_to is quite possibly what you want. - Matt
Matthew Palmer wrote:> If *all* you want to do is use the view from another controller action. > However, if you wanted to use all of the code of the other controller, you''d > need to do something like this in your *view* for first/foo: > > <%= render_component :controller => ''second'', :action => ''index'' %> > > I''ve found this useful for putting intelligent sidebars in my views (where a > partial just won''t cut it). >I''d be interested why you think a partial couldn''t be used for your intelligent sidebar. In my experience, partials can always be used, so a specific example when it can''t would be *very* useful for me. -- stefan -- For rails performance tuning, see: http://railsexpress.de/blog Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml