That''s brilliant... And that solves about 85% of my problems. How would
you call a method to submit data?
Say I have this in Controller a:
def create
@r = Request.new(params[''request'']
if @r.save
flash[:notice] = "Request saved"
redirect_to :action => ''list'',
:pid=>params[''pid'']
else
render :action=>"new"
end
For one reason or another, controller B needs to do the same thing...
Maybe even from the same form. I''m not quite getting how I should call
this from another controller.
I suppose I could just have my form in views/b/new.rhtml post data to
controller A, but that just doesn''t seem right to me (I''d have
to track
which controller they came from so I could route them back...)
Any ideas?
-----Original Message-----
From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Ezra
Zygmuntowicz
Sent: Tuesday, October 18, 2005 10:35 AM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails] Calling other controller methods
On Oct 18, 2005, at 7:55 AM, Hogan, Brian P. wrote:
> What (if any) is the preferred way to call an action in controller
> B from controller A and have execution return to controller A?
>
> Basically, I have a "requests" controller. New requests can be
> made by client users who have their own controller. Since the new
> request code can be called from two places, where do I put the code
> to read the form and do the insert?
>
> Guidance would be greatly appreciated.
Brian-
You could use render_component_as_string like this:
Controller B
def test
#whatever you want to do here to return some results
end
Controller A
def page
@items = MyModel.find :all # or whatever
@action_from_b = render_component_as_string :controller =>
''b'', :action => ''test'', :params => {
:key => ''value'' }
end
This should do a find on MyModel and put the results in @items
for the view, then it will call the test action in controller b with
some extra params(you can leave the params off if you don''t need
them) and put the resulting string in @ action_from_b. Then it will
render yor page.rhtml view of the a controller.
HTH-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails