错误 - 模板丢失,同一帖子不能有 2 个显示视图 [英] Error - Template is missing, unable to have 2 show views for the same posts
问题描述
这是一个后续问题:在一个动作中呈现不同的视图
我得到的错误是:
Template is missing
Missing template items/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
需要注意的是,我将 show.html.erb
文件复制到两个文件 show_with_edit.html.erb
和 show_with_star.erb
>,并删除了 show.html.erb
以避免重复.
It should be noted, that I copied the show.html.erb
file to two files show_with_edit.html.erb
and show_with_star.erb
, and deleted show.html.erb
to avoid duplicates.
我在 posts_controller.rb
def show
if signed_in?
show_signed_in
else
show_not_signed_in
end
end
def show_signed_in
#add methods here
@post = Post.find(params[:id])
respond_to do |format|
format.html # show_with_edit.html.erb
format.json { render json: @post }
end
render 'show_with_edit'
end
def show_not_signed_in
#add methods here
@post = Post.find(params[:id])
respond_to do |format|
format.html # show_with_star.html.erb
format.json { render json: @post }
end
render 'show_with_star'
end
我知道现在两个不同的视图是相同的,我现在只是在那里放了不同的文本.一旦我确定了这一点,我将向每个视图添加它自己的方法和内容等.
I am aware that for now the two different views are identical, I just put there different text for now. Once I have this nailed down I'll add to each view its own methods and content etc.
推荐答案
你把渲染放在了错误的地方.
You've put render in the wrong places.
def show_signed_in
#add methods here
@post = Post.find(params[:id])
respond_to do |format|
format.html # show_with_edit.html.erb
format.json { render json: @post }
end
render 'show_with_edit'
end
应该是
def show_signed_in
#add methods here
@post = Post.find(params[:id])
respond_to do |format|
format.html { render 'show_with_edit' }
format.json { render json: @post }
end
end
注意移动到 format.html 块的 render
.
Note the render
that is moved to the format.html block.
同样适用于 show_not_signed_in.
The same applies for show_not_signed_in.
这篇关于错误 - 模板丢失,同一帖子不能有 2 个显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!