Disable layout rendering in controller filter - ruby-on-rails

Using pjax, trying to disable layout if request.headers['X-PJAX'] is true.
Instead of placing this logic in all my routes, is there a way to setup a filter that does this?
after_filter lambda {
if request.headers['X-PJAX']
# disable rendering with layout
end
}
def show
render layout: application
end

The following should work
SomeController < ApplicationController
layout choose_layout
def actions
.
.
.
private
def choose_layout
request.headers['X-PJAX'].present? ? false : 'application'
end
end
If you only ever use the application layout, I guess you could put it in the Application controller to have it applied to all controllers automatically.

Related

Rails serve page directly from view file without merging into application.html.erb

I have some common HTML for most of the views (like headers and footers), so I put them in application.html.erb file.
Now, I have few pages whose HTML has nothing in common with others, so instead of changing application.html.erb and impacting all the pages, I want a way to load these few pages completely from the view file without involving application.html.erb.
Add render layout: false at the end of your action or if you want to use it for every action in the controller, add layout false at the beginning of the controller.
You have to declare layout false un the controller action like:
def action_name
...
render layout: false
...
end
You can check render option in http://guides.rubyonrails.org/layouts_and_rendering.html#options-for-render
There are many ways to render no layout or other layout.
By default is application.html.erb layout.
-In below code, no layout is called to any actions. Or specified layout is called to all actions.
class UserController < ApplicationController
layout :false
def action
..
end
end
-In below code, only specific action layout is called and for other actions default layout is called.
class UserController < ApplicationController
def action
..
render layout :false
end
end

how to use different layout for different pages in ruby on rails?

i know application.html.erb is the default for every page .i want to use a different layout when user login .i mean the dashboard after login should be of different layout rather than the default one(application.html.erb).
Create new layout eg app/views/layouts/dunno.html.erb. Use in controller
class DashboardController < ApplicationController
layout 'dunno'
end
or per action
class DashboardController < ApplicationController
def index
render layout: 'dunno'
end
end
see docs for details
You can do this in application controller, add this code, I am assuming that you are using devise
layout :layout_by_resource
def layout_by_resource
user_signed_in? ? "my_custom_layout" : "application"
end
In your application_controller.rb file do this, hope it helps.
layout :set_layout
def set_layout
if current_user
'dashboard_layout'
else
'default_layout'
end
end
Details about layouts can be found here.
Using a different layout in the action render call
If most of your actions use the same layout, it makes perfect sense to
define a controller-wide layout as described above. Sometimes you'll
have exceptions where one action wants to use a different layout than
the rest of the controller. You can do this by passing a :layout
option to the render call. For example:
class WeblogController < ActionController::Base
layout "weblog_standard"
def help
render action: "help", layout: "help"
end
end
This will override the controller-wide “weblog_standard” layout, and will render the help
action with the “help” layout instead.
If you are using devise gem, and your goal is to use another layout within devise controllers, have a look at their docs
i wanted two different layouts before signin and after signin,so i have implemented using below code where application_controller changes the layout if user is signed_in else uses different layout....
if you are using devise ,dont forget to add layouts in views/layouts
in application_controller.rb
layout :layout_by_resource
def layout_by_resource
unless user_signed_in?
Rails.logger.info "===========Setting layout as views/layouts/auth.html.erb"
'auth'
else
Rails.logger.info "===========Setting layout as views/layouts/blue.html.erb"
'basic'
end
end

create new page (not having the layout specified in /app/layouts/* files) in rails

Most of my view files have the same layouts, hence it was reasonable to define layouts/application.html.haml (and other header, footer files).
But now I need to create a page that does NOT have any of those styling.
In fact, I just want a plain page with a header.
How do I do that?
I think you're after. In your controller, assuming your action is called myaction
def myaction
# do here whatever you need to do and then
render :layout => false
end
See options for render in Rails Guide: layouts and redendering
You can specify the layout in the controller like so:
class ThingsController < ApplicationController
layout "some_layout"
# rest of the controller
end
This would look for app/views/layouts/some_layout.html.erb
You can also use a method to choose the layout:
class ThingsController < ApplicationController
layout :choose_layout
def choose_layout
current_user.cat? ? "cat" : "dog"
end
# rest of the controller
end

How say to a view to not import default application layout in Rails?

I want to all my views from different paths to use the 'layouts/application.html.erb', except a view that has a specific structure. Is that possible without forcing to create a layout for this view and each one for others?
The 'index.html.erb' couldn`t use the layout 'application.html.erb' in my case.
You can override the default layout by passing an explicit layout name in the render call.
class FoosController < ApplicationController
def index
# call below uses layouts\new_layout.html.erb as the layout
render :layout => 'new_layout'
# if you want to render without a layout
# render :layout => false
end
end
This is easy. If you want to use a different layout for the entire controller, just put the following at the top of that controller:
class ItemsController < ApplicationController
layout "inventory"
#...
end

Completely different home page

I have an application where my homepage is significantly different from other pages. SO can you suggest how I should use my layout?
You can change the layout for all actions in a controller with the following code:
class ThingsController < ApplicationController
layout "my_layout"
...
end
You can change the layout for a specific action using the following code:
def ThingsController < ApplicationController
def action
...
# to render "app/views/things/action"
render :layout => "my_layout"
# or to render a specific view
render "pages/something", :layout => "my_layout"
end
end
If I got it right you are asking about how to manage multiple layouts for your project. What you could do is simply adding your layouts inside the layout directory and by referencing the one you want to use by adding this line to the controller:
layout "name_of_your_layout"

Resources