ruby-on-rails – Rails没有渲染我的应用程序布局

我刚刚在Rails 3.1.1中创建了一个新的rails应用程序,我的应用程序布局没有在浏览器中呈现。渲染的唯一的东西是我放在视图中的代码(例如,views / public / home.html.erb)。

这只是渲染通过<%= yield%&gt例如,localhost:3000 / public / home只显示:

<h1>Homepage</h1>
<h2>Here we go.</h2>

<a href="/#">Visit the login page</a>

这是我的/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>

以下是我的路线:

 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

以下是application_contoller.rb中的内容:

class ApplicationController < ActionController::Base
  protect_from_forgery

end

以下是public_controller.rb中的内容:

class PublicController < ActionController::Base
  protect_from_forgery

  def home
  end

  def secret
  end
end

以下是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

这里是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
我刚刚遇到同样的问题,问题只是一个简单的错误。

您的控制器PublicController是“ActionController :: Base”的子类。除了ApplicationController之外的控制器需要从ApplicationController中进行子类化,以便将其视图呈现在application.html.erb布局中

如果你改变

PublicController < ActionController::Base

PublicController < ApplicationController

它应该工作。

翻译自:https://stackoverflow.com/questions/8865999/rails-is-not-rendering-my-application-layout

转载注明原文:ruby-on-rails – Rails没有渲染我的应用程序布局