Pez
Pez

Reputation: 1101

How to call more then one layouts in rails controller

class UserSignupController < ApplicationController

  layout "signup", only: [:signup]
  layout "user_sessions", only: [:thanks]

  def signup
  end

  def thanks
  end
end

I have two different layouts. I want to call signup layout for signup.It can works fine. But when i give two layouts it crashes my code. Is it possible to give like this?

Upvotes: 2

Views: 229

Answers (2)

rails_id
rails_id

Reputation: 8220

You can add a method to set the layout and put under private.

class UserSignupController < ApplicationController
  layout :specific_layout

  def signup
  end

  def thanks
  end

  private

  def specific_layout
    case action_name
    when "signup"
      "signup"
    when "thanks"
      "user_sessions"
    else
      "otherlayout"
    end
  end
end

Upvotes: 0

PaReeOhNos
PaReeOhNos

Reputation: 4398

You can specify the layout in each action if you want to use separate ones. So for example

def signup
    render "signup", layout: "signup"
end

def thanks
    render "thanks", layout: "thanks"
end

That should do it :)

Upvotes: 2

Related Questions