Rails没有呈现我的应用程序布局

时间:2012-01-14 22:47:53

标签: ruby-on-rails ruby-on-rails-3

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

它只渲染通过<%= yield%>传输的内容。例如,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

2 个答案:

答案 0 :(得分:77)

我自己也遇到了同样的问题,问题只是一个简单的错误。

你的控制器PublicController是子类“ActionController :: Base”。 ApplicationController以外的控制器需要从ApplicationController继承,以便在application.html.erb布局中呈现它们的视图

如果你改变了

PublicController < ActionController::Base

PublicController < ApplicationController

它应该有用。

答案 1 :(得分:9)

最后一个皱纹陷入同样的​​问题:我在子类控制器中重写初始化。确保首先调用'超级':

def initialize
    super
    @some_client = SomeClient.new
end