problems with multiple render and redirect

def test
  foo = redirect_to(:action => "foo").to_s and return
  render :text => foo + " bar"
end

def foo
  render :text => "foo "
end

This code prints
foo
while it should
foo bar
why? thank you

No, that should display "foo " in the browser after the redirected
request comes in to the app. The initial request hits the test action,
which redirects to action foo *and returns* which prevents render :text
=> foo + " bar" from ever being called. This is the correct behavior as
well because you can only render or redirect once per action. On the
second request (from the redirect), the browser will hit the foo action
and display "foo ".

-Bill

slava wrote:

thank you for quick response.
the reason i asked is because i need to call multiple actions from one
controller action in sequence
like
def daily
    log += render or redirect_to or render_to_string(:controller =>
'another_controller', :action => 'someaction')
    log += render or redirect_to or render_to_string(:controller =>
'another_controller', :action => 'someaction')
    ...
    render :text => log
end

i am not sure how to do this. i thought 'and return' at the and of
render call would do it, but that does not work. what is the right
way?

Calling render_xxx doesn't actually execute the code for the specified
action. What are you actually trying to do ?

Fred

I want to execute multiple actions in a row from one call to a
controller, collect results from each into a string for logging. I am
not sure how to make it any more clear than that. I thought my
previous description pretty much said just that. I want to execute a
call to a daily action, which performs a batch of actions and logs
results of each.

You can call other actions from the controller you're running in (since actions are just public instance methods), calling the actions from another controllers is another subject (since you'd have to create the instance of that controller yourself and set it up appropriately).

Sounds like these actions you're calling should actually be model code, at which point it becomes easy.

Fred

Thank you for the answer.
Do you know how I can create instances of the other controllers? I do
need to keep them as controllers (not models) as they are controllers
and sometimes are called directly by clients. Basically I just need to
batch a bunch of calls to multiple controllers in a row, and
collecting results.

thank you.
Slava

I found the answer I was looking for :slight_smile: here
http://matthall.wordpress.com/2006/12/06/how-to-execute-a-rails-controller-action-via-cron/
it turn out I need to add
require 'action_controller/integration' to environment.rb
then run a script with something like
./script/runner -e production "app =
ActionController::Integration::Session.new; app.get 'account/
send_newsletters'"