bypass application.html.erb in Rails app - ruby-on-rails

I have an application.html.erb file which sets out layout for every pages in my app (header, footer etc.) like a typical Rails app.
However, I would like to have a landing page which I don't want to use this file. How should I bypass application.html.erb?
Thank you.

Use
render :layout => false
or
render :layout => 'whatever'
in your action. If you are using a separate LandingController you simply can create a app/views/layouts/landing.html.erb which will be picked up or you can override the layout via
class LandingController < ApplicationController
layout 'whatever'
...
end

You can set a layout in your render function:
render {other arguments}, :layout => :homepage
You can also set that option to false to not use any layout at all.
You can do something similar if you want an entire controller to use a custom layout:
class MyController < ApplicationController
layout :homepage
#...
end
Hope that helps!

In the controller that renders the view, change the render to:
render :layout => false
You can read more about options to render and how to work with layouts at the Rails guide to render and layouts.

Related

Rails 3: How to get the name of the current layout?

In my controller I have:
render :layout => 'mobile'
In my view I want to be able to get the name of the layout, in this case "mobile".
I don't need the controller or action name...just the name of the layout.
I'm using Rails 3.0.6.
I was late, but try this:
controller.send(:_layout).virtual_path.name
Tested in Rails 3.2
why you dont make an variable in your controller:
#layout = 'mobile'
render :layout => #layout
then you can access #layout in your view

How to remove application layout from a particualar Ruby on Rails page?

How can I remove the layout application.html.erb from rendering on a particular page. It is now visible on all pages in my application.
You can override default rendering at the controller level.
class Admin::HomeController < Admin::BaseController
layout "admin"
You can also override rendering of layouts at a controller action level:
def show
render :layout => "layout_for_show_only"
end
And, if you are really desperate, you can override layouts in the view:
<%= render "print_view", :layout => "print" %>
See the excellent rails guide on the subject: layouts and rendering in Rails
ian.
You can simply add to the controller:
layout false, only: [:show, :edit]
which means, that application layout won't be rendered for the show and edit pages
Here is an answer.
You can set: format.js {render :layout=>false}
jQuery + Ajax + Haml. js.erb files not firing

undefined method `layout' for #<HelloController:0x101d7edb0> from Controller

I was trying to switch the layout using Ruby on Rails but I am getting the error: undefined method `layout' for #. I am using Rails 2.3.5 Am I missing an include?
Here is the code:
class HelloController < ApplicationController
def index
layout 'standard'
#message = "Goodbye!"
#count = 3
#bonus = "This is the bonus message!"
end
end
If you are using layout as such, it goes in the Class definition, not an action.
class HelloController < ApplicationController
layout 'standard'
def index
...
This is saying that you want to use this layout for rendering all actions in this controller.
If you want a specific layout for that one action, you would use render :layout as so:
def index
#message =
...
render :layout => 'standard'
end
EDIT: the docs (towards the bottom) seem to suggest that you need to specify the action, as well as the layout when using a specific layout for one action. I don't remember that being the case, but if it is, the above would be render :action => 'index', :layout => 'standard'.

Is it possible to use partials with the layout option inside of a controller?

When try I following code in a controller, the view renders without using the layout
def xyz
render :partial => 'platinum_home', :layout => 'platinum_layout'
end
But If I do the following inside the partial
<% render(:layout => "platinum_layout") do %>
blah blah blah
<% end %>
It works just fine, is the first example not possible using rails?
In your controller at the top add the following:
class SomeController < ApplicationController
layout "platinum_layout", :only => :xyz
Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.
So to use current layout for your just used.
def xyz
render :partial => 'platinum_home', :layout => true
end

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?
The reason this doesn't work is because you can only have global one layout declaration per controller. The :only and :except conditions just differentiate between actions that should get the specified layout and the ones that are excluded get rendered without a layout. In other words, a layout declaration always affects all actions that use default rendering.
To override you simply specify a layout when you render like one of the following examples inside an action:
render :layout => 'static'
render :action => 'privacy', :layout => 'static'
render :layout => false # Don't render a layout
Another option is to define a method for your layout call, like so:
layout :compute_layout
and then
def compute_layout
action_name == "privacy" ? "static" : "sessions"
end
However this is really only useful when you want to determine the layout at runtime based on some runtime parameter (like a variable being set). In your example, that does not seem to be necessary.
You can just specify layout :static where you need it.
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

Resources