mack
mack

Reputation: 2965

Routes to Specific View in Ruby on Rails

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?

Upvotes: 1

Views: 557

Answers (2)

Praveen Perera
Praveen Perera

Reputation: 481

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

Upvotes: 0

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

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

Upvotes: 3

Related Questions