36

I think the code is more explicit

option A

class RedirectController < ApplicationController
  def index
    redirect_to :controller => 'posts', :action => 'show', :id => 1
    # it works
  end
end

option B

class RedirectController < ApplicationController
  def index
    render :controller => 'posts', :action => 'show', :id => 1
    # it doesn't work
  end
end

Is possible in (B) to load another action in another controller? (and not just the view) How? Thanks

user142913
  • 883
  • 1
  • 7
  • 14
  • Why would you want to do this? It might be better if you tell us the underlying problem that you are trying to solve. – John Topley Jul 30 '10 at 09:38
  • The "underlying problem" is that i want to be free to set up some urls as I wish without touching routes.rb – user142913 Jul 30 '10 at 16:50
  • 3
    Just to clarify, your Option A does not render an action from another controller, it answers the current request with a redirect that points the browser to the other controller (therefore results in a second request that hits the other controller). `render :template => 'posts/show'´ however really renders the template of the other controller directly. Of course you need to set up stuff for the template to display (like your @post var). I'm not sure if I understand what you're trying to do, but usually you cannot set up urls without touching routes.rb, since that's what routes.rb is good for. – Zargony Aug 02 '10 at 14:43
  • yes if you don't want to assign @post var you should user "redirect_to" because if you use "render" then code in your action aren't executed "render" just renders other view file with current data to fiil in – Bohdan Aug 04 '10 at 14:41

2 Answers2

63

Try render 'posts/show' or render :template => 'posts/show'

Aurélien Gasser
  • 3,043
  • 1
  • 20
  • 25
Bohdan
  • 8,298
  • 6
  • 41
  • 51
  • Thanks it works in both ways but it isn't very clean because I had to add @post .. @post = Post.find 1 render 'posts/show' – user142913 Jul 30 '10 at 16:47
  • This works, but doesn't render the other controller's template around the action template. Is there a way to force that? – elsurudo Jan 07 '13 at 15:59
  • 3
    you can force the layout with `:layout =>` option – Bohdan Jan 07 '13 at 16:10
  • 2
    This renders the thing associated with the route 'posts/show', it doesn't render the #show action from the #posts controller. Those are two completely different ideas that often overlap. – James Moore Feb 26 '17 at 18:10
  • 1
    Imagine the following line in routes.rb: get 'posts/show', controller: :something_else, action: :erase_the_database. Or imagine a routes.rb that doesn't have a route for posts/show. The question was very specific: it wanted to render a controller/action pair, not a route. (Having said that, I think your answer is useful, even if it's not really an answer to the question that was asked.) – James Moore Feb 26 '17 at 18:17
6

Just render the template

def index
  render 'posts/show'
end

This one also works

def index
  render template: 'posts/show'
end

If you want to render in some other layout

def index
  render template: 'posts/show', layout: 'different_layout' 
end
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88