错误 - 模板丢失,无法为同一帖子显示2个视图 [英] Error - Template is missing, unable to have 2 show views for the same posts

查看:168
本文介绍了错误 - 模板丢失,无法为同一帖子显示2个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题: Rails中同一数据库的两个单独显示视图



我得到的错误是:

 模板缺失

缺少模板项目/ show,application / show with {:locale => [:en],:formats => [: html],:handlers => [:erb,:builder,:coffee]}。

应该注意,我复制了 show.html.erb 文件复制到两个文件 show_with_edit.html.erb show_with_star.erb



我的代码在 posts_controller.rb

  def show 
如果signed_in?
show_signed_in
else
show_not_signed_in
end
end

def show_signed_in
#add方法在这里
@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方法在这里
@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

我知道现在两个不同的视图是一样的,我现在只是把不同的文本。一旦我有了这个钉子,我会添加到每个视图自己的方法和内容等。

解决方案

在错误的地方。

  def show_signed_in 
#add方法在这里
@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方法在这里
@post = Post.find(params [:id])

respond_to do | format |
format.html {render'show_with_edit'}
format.json {render json:@post}
end
end
pre>

请注意将 render 移动到format.html块。



这同样适用于show_not_signed_in。


This is a follow up question to: Two separate show views for the same database in Rails

The error I am getting is:

Template is missing

Missing template items/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.

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.

My Code in 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

should be

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

Note the render that is moved to the format.html block.

The same applies for show_not_signed_in.

这篇关于错误 - 模板丢失,无法为同一帖子显示2个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆