Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails is not rendering my application layout

Tags:

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 
like image 243
Lee McAlilly Avatar asked Jan 14 '12 22:01

Lee McAlilly


People also ask

How can you tell Rails to render without a layout *?

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.

How do you render in rails?

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.

How do you render partials?

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() .

What is the layout in Ruby on Rails?

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.


1 Answers

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.

like image 171
knt Avatar answered Nov 07 '22 00:11

knt