Rajesh Omanakuttan
Rajesh Omanakuttan

Reputation: 6928

Not Rendering the Default Application Layout in Rails 3

I am an entry developer in Rails and I am doing system study of an Application developed in Ruby 1.8.7 and Rails 3.0.3. In my App, I have application_controller with an application.html.erb layout and a home_controller with a home_html.erb layout along with CRUD views. Then in my routes.rb, the default route is set as given below along with resourceful routing for home.

root :to => 'home#index'

resources :home

In application.html.rb, we have default layout to be rendered inside which the other views need to be inserted using <%= yield %>. But I have another layout called home.html.erb whose contents are the same as that of the application.html.erb. When I run the app, the default layout is loaded from home.html.erb instead of application.html.erb. What could be the reason?

Upvotes: 2

Views: 5464

Answers (3)

Rajesh Omanakuttan
Rajesh Omanakuttan

Reputation: 6928

By Default, a controller will first look for a template with the same name inside the layouts folder and if not found, it will render the default application layout.

In this scenario, when home_controller index action is performed, the layout rendered is home.html.rb along with the view content since home.html.rb is with the same naming format as of the controller. When I changed it to homes.html.rb, the controller takes application.html.rb from the layouts folder. So the solution is,

Just remove home.html.erb from views/layouts/ folder. It will automatically render application.html.erb by default.

Thanks everyone.

Upvotes: 2

sameera207
sameera207

Reputation: 16629

By default application layout will be called to all of your controllers, but if you want to render a specific layout you should do this

class HomeController < ApplicationController
   layout 'home'
end

HTH

Upvotes: 2

Sachin R
Sachin R

Reputation: 11876

In your controller write

   class TetsController < ApplicationController
      layout "layout_name"
      #...
    end

Upvotes: 5

Related Questions