开发者

Use a layout for a specific action

开发者 https://www.devze.com 2023-02-21 20:46 出处:网络
If I would like to use a layout for a certain action (say, the show action) that is different from the layout declared at the top开发者_StackOverflow社区 of the controller.rb file, how could I do this

If I would like to use a layout for a certain action (say, the show action) that is different from the layout declared at the top开发者_StackOverflow社区 of the controller.rb file, how could I do this? This must be possible in rails, but I cannot seem to find anything about it.


  render :layout => 'otherlayout'


layout 'layout', :only => [:first_action, :second_action]
layout 'second_layout', :only => [:third_action, :fourth_action]

Don's is right as well, just depends on your application which is more DRY (or DRY-er?)


EDIT My previous code is mistaken. You cannot specify the layout function multiple times. I found this solution online for dynamic layout rendering:

class OrdersController < BaseController
  layout :determine_layout

private
  def determine_layout
    %w(new).include?(action_name) ? "some_layout" : "public"
  end
end

Source: apidock.com/rails/Actio...


Following example applies desired layout to specific action, otherwise, it uses default layout (layouts/application.html.erb).

class ArticlesController < ApplicationController
   layout "article_editor", only: [:new, :edit]

   def index
     # default layout
   end

   def new
     # article_editor layout
   end

   def edit
     # article_editor layout
   end
end
0

精彩评论

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

关注公众号