Rails 没有呈现我的应用程序布局 [英] Rails is not rendering my application layout
问题描述
我刚刚在 Rails 3.1.1 中创建了一个新的 rails 应用程序,我的应用程序布局没有在浏览器中呈现.唯一呈现的是我放在视图中的代码(例如 views/public/home.html.erb).
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).
它只渲染通过 <%= yield %> 管道传输的内容.例如, localhost:3000/public/home 只显示这个:
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>
这是我的/layouts/application.html.erb 中的内容:
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>
这也是我的路线:
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 中的内容:
Here's what's in application_contoller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery
end
这是 public_controller.rb 中的内容:
Here's what's in public_controller.rb:
class PublicController < ActionController::Base
protect_from_forgery
def home
end
def secret
end
end
这是 session_contoller.rb 中的内容:
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
这是 users_controller.rb 中的内容:
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.
您的控制器 PublicController 正在继承ActionController::Base".除了 ApplicationController 之外的控制器需要从 ApplicationController 继承子类,以便在 application.html.erb 布局中呈现它们的视图
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
如果你改变了
PublicController < ActionController::Base
到
PublicController < ApplicationController
它应该可以工作.
这篇关于Rails 没有呈现我的应用程序布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!