Topic: is there any server side forward?

Hi all,
there is a client side redirect in ruby on rails  , for example.
redirect_to :controller => 'my_controller', :action => 'my_action'.
like that i want to know whether there is a server side forward, like in jsp(jsp:forward).
any help would be appreciated.
thanks,
dhanasekaran

Re: is there any server side forward?

I'd say that redirect_to is a server side forward

--

Re: is there any server side forward?

if redirect_to is server side forward , then there should not be a change in url ,
suppose say i am having following action in my controller

def create
redirect_to :controller => 'my_controller', :action => 'create_new'
end

and if i type the url like http://localhost:3000/my_controller/create
the url will be changed to http://localhost:3000/my_controller/create_new.
that means it is doing the client redirect.

if it is a server side forward there wont be any change in the url.
hope i am clear.

so what i am saying is redirect_to is a client side redirect , and NOT a server side forward.

thanks,
dhanasekaran

Re: is there any server side forward?

I see what you are after, dont know how/if to do that in rails.

however the redirect is happing in the controller file not the html like javascript. so I dont really see how its client side. http://localhost:3000/my_controller/create, just specifies the method to be used. if there is no create.rhtml file there will be no output on the clients browser.

you could use
render_action "create_new" if you did'nt want any change in the url

Last edited by BasicMind (2006-11-22 10:43:05)

--

Re: is there any server side forward?

The redirect_to method sends a 302 HTTP response back to the client, so yes, it is a client-side redirect. If you want a server-side redirect, you can do this with routes:

# in routes.rb
map.connect 'my_controller/create', :controller => 'my_controller', :action => 'create_new'

"render :action" is also a decent solution depending on your situation.

Railscasts - Free Ruby on Rails Screencasts

Re: is there any server side forward?

render is probably the closest thing to JSP's request forwarding.

vinnie - rails forum admin

Re: is there any server side forward?

hi all,
thanks all for the reply.
regding render :action , it will not execute the action defined in the controller, just it will render the :action.rhtml. one alternative is that i can call the action inside some other action and use render :action
for example

def create
  # do something
end
def create_new
  create
  render :action => 'create'
end

my problem is that i want to execute action defined in controller2 from action defined in
controller1.i.e.both actions are not in the same controller. anybody have any ideas?
and regding map.connect, say example
map.connect 'my_controller/create', :controller => 'my_controller_new', :action => 'create_new'

if there is a controller 'my_controller' and action 'create' available it will not execute
my_controller_new.create_new.
using map.connect, my_controller_new.create_new is executed only if there is no my_controller and create action.
thanks,
dhanasekaran

Re: is there any server side forward?

dhanasekara wrote:

and regding map.connect, say example

map.connect 'my_controller/create', :controller => 'my_controller_new', :action => 'create_new'

if there is a controller 'my_controller' and action 'create' available it will not execute
my_controller_new.create_new.
using map.connect, my_controller_new.create_new is executed only if there is no my_controller and create action.
thanks,

Did you place that line above the standard :controller/:action/:id line?

Railscasts - Free Ruby on Rails Screencasts

Re: is there any server side forward?

thanks for pointing it out
if i place above the standard :controller/:action/:id,
it is working fine

map.connect 'my_controller/create', :controller => 'my_controller_new', :action => 'create_new'

but there is one issue.
the 'create' action defined in my_controller is not executed, it straight away executes 'create_new' action defined in 'my_controller_new' controller.
my requirement is execute 'my_controller/create' first and then execute 'my_controller_new/create_new',
any pointers would be appreciated

Last edited by dhanasekara (2006-11-29 09:41:30)

Re: is there any server side forward?

I don't think that's possible with Rails. What are you doing which requires this kind of redirect? Perhaps it can be better handled some other way...

Railscasts - Free Ruby on Rails Screencasts

Re: is there any server side forward?

how to do server side forwarding to a different website in rails?

thanks,
ricky