Is there a way to chain controllers together? For example in one controller I have a save() method and when thats done I want to go and execute the code for the index() method. I don''t want to use a redirect. In the Java world I can do this with a concept called "request forwarding". Is this something that is possible with Ruby on Rails? Is it as simple as just calling the index() method at the end of my save() method. TIA. Scott. -- Scott F. Walter Scott F. Walter Principal Consultant Vivare, Inc. E: scott.walter-uosFFu51klvQT0dZR+AlfA@public.gmane.org E: scott-APWf0AbNa2kIjDr1QQGPvw@public.gmane.org Visit scottwalter.com <http://scottwalter.com> --Point. Click. Explore!
At the end of your save method you could have a redirect_to, which would be transparent to your end-users and do what you need. Something like: @item = item.new @item.save redirect_to :controller => "secondcontroller", :action => "index" I guess there may be other solutions, but this is what I''ve been using. Fred On 9/9/05, Scott F. Walter <scott.walter-uosFFu51klvQT0dZR+AlfA@public.gmane.org> wrote:> Is there a way to chain controllers together? For example in one > controller I have a save() method and when thats done I want to go and > execute the code for the index() method. I don''t want to use a > redirect. In the Java world I can do this with a concept called > "request forwarding". Is this something that is possible with Ruby on > Rails? Is it as simple as just calling the index() method at the end of > my save() method. > > TIA. Scott. > -- > > Scott F. Walter Scott F. Walter > Principal Consultant > Vivare, Inc. > > E: scott.walter-uosFFu51klvQT0dZR+AlfA@public.gmane.org > E: scott-APWf0AbNa2kIjDr1QQGPvw@public.gmane.org > Visit scottwalter.com <http://scottwalter.com> --Point. Click. Explore! > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
You can do something like this... def save ... index render :action => ''index'' end On Sep 9, 2005, at 10:50 AM, Scott F. Walter wrote:> Is there a way to chain controllers together? For example in one > controller I have a save() method and when thats done I want to go > and execute the code for the index() method. I don''t want to use > a redirect. In the Java world I can do this with a concept called > "request forwarding". Is this something that is possible with Ruby > on Rails? Is it as simple as just calling the index() method at > the end of my save() method. > > TIA. Scott. > -- > > Scott F. Walter Scott F. Walter > Principal Consultant > Vivare, Inc. > > E: scott.walter-uosFFu51klvQT0dZR+AlfA@public.gmane.org > E: scott-APWf0AbNa2kIjDr1QQGPvw@public.gmane.org > Visit scottwalter.com <http://scottwalter.com> --Point. Click. > Explore! > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Fri, 2005-09-09 at 11:50, Scott F. Walter wrote:> Is there a way to chain controllers together? For example in one > controller I have a save() method and when thats done I want to go and > execute the code for the index() method. I don''t want to use a > redirect. In the Java world I can do this with a concept called > "request forwarding". Is this something that is possible with Ruby on > Rails? Is it as simple as just calling the index() method at the end of > my save() method. > > TIA. Scott.I have a solution that I''d love to have feedback on. I''ve added a method to my ApplicationController like so (expanded somewhat for readability): class ApplicationController < ActionController::Base hide_action :collaborate_with def collaborate_with(options) options[:controller] ||= controller_name save_params = @request.parameters.clone sub_request = @request.clone my_response = @response.clone sub_request.parameters.update options name_of_other = "#{options[:controller].capitalize}Controller" collaborator = Object.const_get(name_of_other).new collaborator.process(sub_request,my_response) @request.parameters.clear.update save_params my_response.body end end And then anywhere I want I can write things like this class PolkaController < ApplicationController def show : @help_text = collaborate_with( :controller => ''wiki'', :action => ''html'', :id => ''PolkaHelp'' ) (html is a method of Sqliki that returns just the rendered content of a page, for embedding, Ajax, etc.). The reason for doing it this way: * It encapsulates the functionality with * the part that is fussy and cryptic all in one place * what''s scattered about is clear & easy to read * All the regular security checking gets done * It fits into a larger scheme (boxcars) that I''m trying to develop * I get to practice spelling "collaborate" correctly But the code above is less than 48 hours old. Does anybody have any ideas for better ways to do this? Does anybody (other than Why T.L.S.) want to tell me I''m crazy? --MarkusQ
Benjamin Curtis napsal(a):> You can do something like this... > > def save > ... > index > render :action => ''index'' > endWhen not using redirection you need to handle cases when user reloads page and reposts data. Normally this would initiate save with same data. -- Kamil
On Fri, 2005-09-09 at 11:50, Scott F. Walter wrote:> > Is there a way to chain controllers together?On Sat, 2005-09-10 at 14:28, Markus wrote:> I have a solution...I''ve added a method to my > ApplicationController [called] collaborate_with...After using my home-grown solution for a while I discovered that I had essentially reimplemented "components", which should also do what you are wanting quite nicely. See http://api.rubyonrails.com/classes/ActionController/Components.html for details. Basically, you can either hand off (internally) to another controller or you can get the response from the other controller and use it as you see fit. --MarkusQ