【发布时间】:2014-05-02 00:37:13
【问题描述】:
我的 rails 应用程序中的一组视图存在一些问题。我创建了运行脚手架来创建帖子模型、控制器和视图。他们自己工作得很好,但他们拒绝引入 application.html.erb 布局。
这是帖子控制器:
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
这是我要使用应用程序布局的视图之一:
<%= 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>
我在这里看到了一篇关于具有
希望这不是一个愚蠢的问题,我还是 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
页面控制器看起来很相似,并且这些页面正确地拉动了 application.html.erb,无需任何特殊工作。
这是 github 上的 repo,https://github.com/CoreyT355/website1。也许这会有所帮助。
【问题讨论】:
-
application.html.erb在目录结构中的什么位置?它发生在所有控制器上,还是仅发生在一个控制器中,或仅发生在控制器中的一个动作(如果是,是哪个动作?) -
你的
application.html.erb的文件结构是什么?应该是app/views/layouts/application.html.erb -
您的应用程序
ApplicationController应该继承自ActionController::Base。它应该看起来像这样:ApplicationController < ActionController::Base -
用来自 cmets 的更多信息编辑了帖子
-
该文件是否应该命名为
application_html.erb?这是更新问题时的错字吗? Rails 通常希望文件命名为application.html.erb
标签: ruby-on-rails ruby-on-rails-4