Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render controller action from another controller

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

like image 267
user142913 Avatar asked Jul 30 '10 09:07

user142913


2 Answers

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

like image 91
Bohdan Avatar answered Oct 20 '22 14:10

Bohdan


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
like image 33
Deepak Mahakale Avatar answered Oct 20 '22 13:10

Deepak Mahakale