Thibaud Clement
Thibaud Clement

Reputation: 6897

Rails 4: applying different layouts to specific controller actions

In my Rails 4 app, I have a PagesController, with a app/views/layouts/pages.html.erb layout.

Two pages need a specific layout: home and help.

To apply a specific layout to home, I did the following:

class PagesController < ApplicationController
  def home
    render :layout => 'homepage'
  end
end

and I have a specific homepage layout: app/views/layouts/homepage.html.erb

This works perfectly.

Then, I tried to replicate this process for the help page:

class PagesController < ApplicationController
  def home
    render :layout => 'homepage'
  end
  def help
    render :layout => 'helppage'
  end
end

and I have a specific helppage layout: app/views/layouts/helppage.html.erb

However, the help page does not take the desired layout.

Is there something wrong with my code?

—————

UPDATE: as per Adrian Mann's request, here is my full PagesController:

class PagesController < ApplicationController

  before_action :authenticate_user!, :only => [:help]

  def home
    [...]
    render :layout => 'homepage'
  end

  def features
  end

  def pricing
  end

  def about
  end

  def contact
  end

  def feedback
  end

  def press
  end

  def help
    render :layout => 'helppage'
  end

  def privacy
  end

  def terms
  end

  def career
  end

  def help
  end

  def faq
  end

  def guides
  end

  def support
  end

  def investors
  end

end

and here is my ApplicationController:

class ApplicationController < ActionController::Base

  # include Pundit

  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  # after_action :verify_authorized, :except => :index

  def after_sign_in_path_for(resource)
    sign_in_url = new_user_session_url
    if request.referer == sign_in_url
      super
    else
      stored_location_for(resource) || request.referer || root_path
    end
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :company_name, :email, :password, :password_confirmation, :time_zone, :company_type, :expert, :expert_approval, :category, :description, :portfolio, :price, :avatar, :first_reference, :second_reference, :third_reference, :location) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :company_name, :email, :password, :current_password, :password_confirmation, :time_zone, :company_type, :expert, :expert_approval, :category, :description, :portfolio, :price, :avatar, :first_reference, :second_reference, :third_reference, :location) }
  end

  def user_for_paper_trail
    user_signed_in? ? current_user.try(:id) : "Unknow User"
  end

  def facebook_copy_link(string)
    require "uri"
    array = URI.extract(string.to_s)
    array.select { |item| item.include? ( "http" || "www") }.first
  end

end

Upvotes: 0

Views: 626

Answers (2)

atw
atw

Reputation: 5830

Try the following, note the removed render:

class PagesController < ApplicationController

  def home
    layout "homepage"
  end
  def help
    layout "helppage"
  end
end

Or:

class PagesController < ApplicationController
  layout "homepage", only: [:home]
  layout "helppage", only: [:help]

  def home
  end

  def help
  end
end

As stated elsewhere:

The method render will actually attempt to render content; you should not call it when all you want to do is set the layout.

Upvotes: 2

archana
archana

Reputation: 1272

I don't see anything wrong in your code. Which layout do you see for help action? Can you try layout false?

class PagesController < ApplicationController
  layout false

  def home
    render :layout => 'homepage'
  end
  def help
    render :layout => 'helppage'
 end

end

Upvotes: 0

Related Questions