40

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

4
  • Why would you want to do this? It might be better if you tell us the underlying problem that you are trying to solve. Commented Jul 30, 2010 at 9:38
  • The "underlying problem" is that i want to be free to set up some urls as I wish without touching routes.rb Commented Jul 30, 2010 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. Commented Aug 2, 2010 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 Commented Aug 4, 2010 at 14:41

2 Answers 2

68

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

Sign up to request clarification or add additional context in comments.

5 Comments

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'
This works, but doesn't render the other controller's template around the action template. Is there a way to force that?
you can force the layout with :layout => option
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.
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.)
9

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.