Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails layouts...except and only bug

I have a controller with the following layout logic

layout 'sessions', :except => :privacy
  layout 'static', :only => :privacy

The issue is that Rails seems to ignore the first line of code and the layout "sessions" is not applied for any actions. It simply thinks to render the static layout for privacy and no layout for the rest.

Anyone know how to fix this?

like image 278
Tony Avatar asked Oct 16 '09 01:10

Tony


People also ask

How can you tell Rails to render without a layout?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.

What is the layout in Ruby on Rails?

A layout defines the surroundings of an HTML page. It's the place to define a common look and feel of your final output. Layout files reside in app/views/layouts. The process involves defining a layout template and then letting the controller know that it exists and to use it.

How should you use nested layouts in Rails?

Rails provides us great functionality for managing layouts in a web application. The layouts removes code duplication in view layer. You are able to slice all your application pages to blocks such as header, footer, sidebar, body and etc.


1 Answers

You can also dynamically determine the layout within your controller:

class SampleController < ApplicationController

    layout Proc.new { |controller| (controller.action_name == 'privacy') ? 'static' : 'sessions' }

    ...
end

If more actions within the controller are sharing the same layout:

class SampleController < ApplicationController

    layout Proc.new { |controller| ['action1', 'action2'].include?(controller.action_name) ? 'layout1' : 'layout2' }

    ...
end

Source: https://guides.rubyonrails.org/layouts_and_rendering.html#using-render

like image 178
aruanoc Avatar answered Sep 24 '22 12:09

aruanoc