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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With