开发者

Render controller action from another controller

开发者 https://www.devze.com 2023-01-09 07:23 出处:网络
I think the code is more explicit option A class RedirectController < ApplicationController def index

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 i开发者_JAVA技巧n (B) to load another action in another controller? (and not just the view) How? Thanks


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消