gpt4 book ai didi

ruby-on-rails - Ruby on Rails View 未拉入 application.html.erb 布局

转载 作者:行者123 更新时间:2023-12-02 21:26:59 25 4
gpt4 key购买 nike

我的 Rails 应用程序中的一组 View 遇到一些问题。我创建了运行脚手架来创建帖子模型、 Controller 和 View 。它们自己工作得很好,但它们拒绝引入 application.html.erb 布局。

这是帖子 Controller :

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]

# GET /posts
# GET /posts.json
def index
@posts = Post.all
end

# GET /posts/1
# GET /posts/1.json
def show
end

# GET /posts/new
def new
@post = Post.new
end

# GET /posts/1/edit
def edit
end

# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body, :datecreated, :datemodified, :published, :author)
end
end

这是我想使用应用程序布局的 View 之一:

<%= render 'form' %>

这是 application.html.erb:

<!DOCTYPE html>
<html>
<head>
<title>Coreyonrails</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>

我在这里看到了一篇关于具有 < ActionController::Base 的 Controller 的子类的文章我的 Controller 没有,现在仍然没有,所以没有帮助。

希望这不是一个愚蠢的问题,我对 Rails 还很陌生。

编辑以显示文件结构。

app
controllers
application_controller.rb
pages_controller.rb
posts_controller.rb
views
layouts
_header.html.erb
application.html.erb
pages
about.html.erb
contact.html.erb
home.html.erb
posts
_form.html.erb
_posts.html.erb
edit.html.erb
index.html.erb
new.html.erb
show.html.erb

其余的 Rails 生成的文件夹也在那里,我只是展示了与问题相关的内容。

ApplicationController 确实继承自 ActionController::Base,这是整个文件:

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end

页面 Controller 看起来很相似,并且这些页面可以正确拉取 application.html.erb,无需任何特殊工作。

这是 github 上的存储库,https://github.com/CoreyT355/website1 。也许这会有更多帮助。

最佳答案

一些事情(应该是评论,但为了清楚起见,我将其写在这里):

  1. Check you have app/views/layouts/application.html.erb
  2. Check you have app/controllers/application_controller.rb inheriting from ActionController::Base

当您提到 <%= render "form" %> 的使用时, 你是什么意思?这是在哪里渲染的?如果它是通过操作呈现的,它将自动显示在您的应用程序布局上

您能为我们发布更多代码吗,特别是 posts_controller , application_controller , routesapplication布局文件?

关于ruby-on-rails - Ruby on Rails View 未拉入 application.html.erb 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23418982/

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