简体   繁体   中英

Rails is not rendering my application layout

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 (eg 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

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.

Ran into the same problem with one final wrinkle: I was overriding initialize in the subclassed controller. Make sure to call 'super' first thing:

def initialize
    super
    @some_client = SomeClient.new
end

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