简体   繁体   中英

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?

In your controller write

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM