I just created a new rails app in Rails 3.1.1, and my application layout is not being rendered in the browser. The only thing that is rendered is the code that I put in the views (e.g. views/public/home.html.erb).
It's only rendering what is being piped through <%= yield %>. For instance, localhost:3000/public/home is on only displaying this:
<h1>Homepage</h1> <h2>Here we go.</h2> <a href="/#">Visit the login page</a>
Here's what's in my /layouts/application.html.erb:
<!DOCTYPE html> <html> <head> <title>My App</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> </head> <body> <ul class="user_nav"> <% if current_user %> <li> Logged in as <%= current_user.email %>. </li> <li> <%= link_to "Log out", logout_path %> </li> <% else %> <li> <%= link_to "Sign up", signup_path %> </li> <li> <%= link_to "Log in", login_path %> </li> <% end %> </ul> <% flash.each do |name, msg| %> <%= content_tag :div, msg, :id => "flash#{name}" %> <% end %> <%= yield %> <h1>test!</h1> </body> </html>
Here are my routes as well:
root :to => "public#home" match "/secret" => "public#secret" get "logout" => "sessions#destroy", :as => "logout" get "login" => "sessions#new", :as => "login" get "signup" => "users#new", :as => "signup" resources :users resources :sessions
Here's what's in application_contoller.rb:
class ApplicationController < ActionController::Base protect_from_forgery end
Here's what's in public_controller.rb:
class PublicController < ActionController::Base protect_from_forgery def home end def secret end end
Here's what's in sessions_contoller.rb:
class SessionsController < ApplicationController def new end def create user = login(params[:email], params[:password], params[:remember_me]) if user redirect_back_or_to root_path, :notice => "Logged in!" else flash.now.alert = "Email or password was invalid" render :new end end def destroy logout redirect_to root_path, :notice => "Logged out" end end
And here's what's in users_controller.rb:
class UsersController < ApplicationController def new @user = User.new end def create @user = User.new(params[:user]) if @user.save redirect_to root_path, :notice => "Signed up!" else render :new end end end
By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.
From the controller's point of view, there are three ways to create an HTTP response: Call render to create a full response to send back to the browser. Call redirect_to to send an HTTP redirect status code to the browser. Call head to create a response consisting solely of HTTP headers to send back to the browser.
Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .
A layout defines the surroundings of an HTML page. It's the place to define a common look and feel of your final output. Layout files reside in app/views/layouts. The process involves defining a layout template and then letting the controller know that it exists and to use it.
I just ran into this same problem myself and the problem is just a simple mistake.
Your controller PublicController is subclassing "ActionController::Base". Controllers other than your ApplicationController need to subclass from ApplicationController in order for their views to be rendered within the application.html.erb layout
If you change
PublicController < ActionController::Base
to
PublicController < ApplicationController
it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With