Pierre P.
Pierre P.

Reputation: 1065

Different application layout for my rails application

I have a home page with its own style (put in application.css) but the rest of the website will be totally different.

Yet, it seems that Rails work with a "parent" file application.html.erb and we can basically use yield to include other .html.erb pages.

However, it's not viable for my application because as I said the home page will have a totally different structure from the rest of my web app and I can't use a common application.html.erb file.

What are the solutions for this situation?

Upvotes: 0

Views: 768

Answers (2)

Rockwell Rice
Rockwell Rice

Reputation: 3002

You can specify the layout controller wide or for a specific action.

The layout file needs to be in the layouts folder.

To specify the layout file controller wide you can do this

class BlogController < ActionController::Base
  layout "unique_blog_layout"

  def index
  end

  ...

end

To set it for a single view

class BlogController < ActionController::Base

  def index
    render layout: "unique_blog_layout"
  end

  ...
end

For a full rundown checkout the docs here and maybe here too.

Upvotes: 2

Mate Solymosi
Mate Solymosi

Reputation: 5977

To use a different layout for just a single template, do this:

  1. Add your new layout to views/layouts/name_of_my_new_layout.html.erb
  2. Use render :index, layout: :name_of_my_new_layout in the controller to tell Rails to use your new layout to render the template

Upvotes: 2

Related Questions