Samuel
Samuel

Reputation: 6136

Devise customisation of views

Is there a way to route devise users to a login and signup screen that doesn't output the header and footer? as everything is being yielded to my application.html.erb

Heres my current code for application.html.erb

 <body>
   <div id="wrap">
    <%= render 'layouts/header' %>
      <%= yield %>
    <div id="push"></div>
   </div>
 <%= render 'layouts/footer' %>
 </body>

Upvotes: 0

Views: 466

Answers (3)

Marco
Marco

Reputation: 1

If you're using Devise with the standard layouts, they're render into Main view: <%= yield %>.

So you can change your application.html.erb in:

  <body>

   <div id="wrap">

     <% if user_signed_in? %>
       <%= render 'layouts/header' %>
     <% end %>

     <%= yield %>

     <% if user_signed_in? %>
       <%= render 'layouts/footer' %>
     <% end %>

   </div>

 </body>

And your application_controller.rb in:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  #before_action :authenticate_user!, except: [:index, :home]
  before_action :authenticate_user!
end

Upvotes: 0

Aluxzi
Aluxzi

Reputation: 177

Controllers support :only and :except options for layouts so you can restrict access in the controller like this:

class RandomController < ApplicationController
layout 'application', :except => [:signup, :signin]

def signin
// some code
end

def signup
// some code
end

end

I'd recommend you view the official RoR website section (this link) on rendering views.

Update 2

Set the layout for specific Devise controllers using a callback in config/application.rb. (so this code belongs in the /config/application.rb file)

This allows for layouts to be specified on a per-controller basis. If, for example, you want a specific layout assigned to Devise::HomeController views:

config.to_prepare do
Devise::HomeController.layout "layout_for_home_controller" 
end

A more indepth example using four different layouts, one for each controller:

config.to_prepare do
Devise::HomeController.layout "layout1"
Devise::UsersController.layout "layout2"
Devise::ArticlesController.layout "layout3"            
Devise::TutorialsController.layout "layout4"        
end

Upvotes: 1

Pierre
Pierre

Reputation: 1114

Not the prettiest solution, but this is a way of doing it.

     <body>
       <div id="wrap">
          <%= render 'layouts/header' unless params[:controller] == "devise/sessions" or "devise/registrations" and params[:action] == "new" %>
          <%= yield %>
        <div id="push"></div>
       </div>
     <%= render 'layouts/footer' unless params[:controller] == "devise/sessions" or "devise/registrations" and params[:action] == "new" %>
     </body>

EDIT:

application_helper.rb

def hidden_header_footer
    params[:controller] == "devise/sessions" or "devise/registrations" and params[:action] == "new"
end

application.html.erb

<%= render 'layouts/footer' unless hidden_header_footer %>
<%= render 'layouts/header' unless hidden_header_footer %>

Upvotes: 0

Related Questions