ruby-on-rails - 模板丢失错误 - 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
comments.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
记录时验证失败时呈现它。
推荐阅读
- python - 我的函数 on_member_join(member) 从未被调用
- encoding - 是否需要添加霍夫曼树或频率表?
- docker - 在 AWS Fargate 上运行的基于 alpine 的 docker 容器内部的 NFS 文件共享挂载
- docusaurus - 是否可以检测 docusaurus 是处于亮模式还是暗模式?
- python - 我正在尝试使用 numpy 模块创建和实现一个识别 Python 数据集中异常值的函数,不断收到“ValueError”
- ios - 在 App Store Connect 上的功能下看不到 App 购买部分
- swift - 如何计算使用 Cloud Firebase 在 Swift UI 中选择一个选项的人数?
- reactjs - 如何使选择选项的值等于选择选项的名称
- laravel - 在父子表 laravel 中插入行
- azure - Azure Monitor 数据收集器 API Powershell 示例不起作用