I'm learning Ruby on Rails. I have a login page that has a layout that is completely different than the rest of the site. Inside of my routes.rb, how to I tell the application to always render this particular page using the "login" view instead of the default "application" view?
Asked
Active
Viewed 495 times
1
-
2You can handle it in your login controller, see this link: http://stackoverflow.com/questions/3025784/rails-layouts-per-action – ifma Aug 23 '15 at 02:28
-
Can it not be done in the routes.rb for some reason? – mack Aug 24 '15 at 02:44
-
1Nope. Looks like a design choice of the framework, see: http://stackoverflow.com/questions/24982111/can-i-render-a-layout-directly-from-routes-rb-without-a-controller – ifma Aug 24 '15 at 02:47
-
Thanks for the link that was very helpful – mack Aug 25 '15 at 13:31
2 Answers
3
In Rails 4, you can use: render layout: 'some_layout'
to render a specific layout.
In your controller's login
method, you can have something like this:
def login
# do stuff
if some_condition
# do stuff
render layout: 'some_condition_layout'
else
# do other stuff
render layout: 'some_other_layout'
end
end
For more information on renderings and layouts, you can check out Layouts and Rendering in Rails

K M Rakibul Islam
- 33,760
- 12
- 89
- 110
0
You can call render layout on each action as per answer above or you can do the following to dynamically set the layout name depending on the action name::
class PagesController < ApplicationController
layout :resolve_layout
def index
end
def home
end
def dashboard
end
private
def resolve_layout
case action_name
when "home" #action name
"home" #layout name
when "dashboard"
"dashboard"
else
"application"
end
end
end

Community
- 1
- 1

Praveen Perera
- 158
- 3
- 13