gpt4 book ai didi

ruby-on-rails - Rails没有呈现我的应用程序布局

转载 作者:行者123 更新时间:2023-12-03 12:21:29 24 4
gpt4 key购买 nike

我刚刚在Rails 3.1.1中创建了一个新的Rails应用程序,并且我的应用程序布局未在浏览器中呈现。唯一呈现的是我放入 View 中的代码(例如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

最佳答案

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

您的 Controller PublicController子类化为“ActionController::Base”。除了ApplicationController之外的其他 Controller 都需要从ApplicationController继承子类,以便将它们的 View 呈现在application.html.erb布局中

如果你改变

PublicController < ActionController::Base


PublicController < ApplicationController

它应该工作。

关于ruby-on-rails - Rails没有呈现我的应用程序布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8865999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com