繁体   English   中英

模板丢失错误 - Ruby on Rails

[英]Template is missing error - Ruby on Rails

我的项目有一个用户添加的书籍列表。 用户可以对添加的书籍发表评论。 评论时要求“标题”和“内容”。

但是,对于comment.rb模型中的这两个属性

    validates :title, presence: true
    validates :content, presence: true

添加时出现此错误。 当我删除这些功能时,我没有收到任何错误。

Missing template comments/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

评论控制器.rb

def create
  @book = Book.find(params[:book_id])
  @comment = @book.comments.new(comment_params)
  @comment.user_id = current_user.id
  @comment.status = "unapproved"
  if @comment.save
    redirect_to book_path(@book), notice: "Comment was successfully created."
  else
    render :new #It gives an error here
  end
end

def comment_params
  params.require(:comment).permit(:title, :content, :book_id, :status,:user_id)
end

评论.rb模型

class Comment < ApplicationRecord
  belongs_to :book
  belongs_to :user
  scope :approved, -> { where status: 'approved'}
  scope :unapproved, -> { where status: 'unapproved'}
  validates :title, presence: true
  validates :content, presence: true
end

Rails 正在抛出一个Exception因为它试图从操作中呈现一个文件,而不是在它检查的默认路径中找到它。

根据您在控制器中的代码,必须存在app/views/comments/new.html.erb文件,以便控制器操作可以在创建comment记录时验证失败时呈现它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM