gpt4 book ai didi

ruby-on-rails - 什么错误会导致 rails 重定向回表单而不写入数据库

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:58 24 4
gpt4 key购买 nike

我有一个名为 Template Controller 的 Controller ,它是从脚手架生成的,它将一个简单的模板模型保存到 mongo 数据库中,出于某种原因,我一直遇到这个问题:当我去保存模板/新 View 时,它不保存到数据库,返回“查看下面的问题”(风格好像我留下了一些空白)。抱歉,如果我遗漏了任何重要信息,如果我需要其他任何信息,请告诉我。我今天几个小时都被这个难住了!

这是日志示例:

由 TemplatesController 处理#create as HTML
参数:{"utf8"=>"✓", "authenticity_token"=>"MnV8IoD92XSAGA2pUZr2PXwO/os8bgm/4ouT7baT+hA=", "template"=>{"task_name"=>"10", "task_type"=>"calls ", "number_to_complete"=>"10", "description"=>"10"}, "commit"=>"Create Template"}
MOPED:127.0.0.1:27017 QUERY database=local collection=users selector={"$query"=>{"_id"=>"52e16f81bd9eb69d4a000001"}, "$orderby"=>{:_id=>1}} flags= [] limit=-1 skip=0 batch_size=nil fields=nil (0.7575ms)
呈现的模板/_form.html.haml(19.0 毫秒)
在布局/应用程序中呈现模板/new.html.haml(21.6 毫秒)
MOPED:127.0.0.1:27017 QUERY database=local collection=organizations selector={"$query"=>{"_id"=>"52c6d7daf13b975c79000002"}, "$orderby"=>{:_id=>1}} flags= [] limit=-1 skip=0 batch_size=nil fields=nil (0.3543ms)
在 229.4 毫秒内完成 200 OK(查看:219.6 毫秒)

我的模型:

class Template < Task
include Mongoid::Document
include Mongoid::Timestamps
field :task_name, type: String
field :task_type, type: String
field :number_to_complete, type: Integer
field :description, type: String
field :id, type: Integer
attr_accessible :about_attributes, :task_name, :task_type, :number_to_complete, :description, :id

end

和 Controller :

class TemplatesController < InheritedResources::Base
def new
@template = Template.new
@task_types = Task.task_type
end
end

最佳答案

如果您想知道哪些验证错误阻止了模型的保存,您必须以某种方式输出它们。

一个快速而肮脏的解决方案:

<%= @template.errors.full_messages.join(', ') %>

但除了简单的调试目的外,最好提供更复杂的反馈,例如突出显示无效的输入字段并在下方显示错误。

关于ruby-on-rails - 什么错误会导致 rails 重定向回表单而不写入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22101785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com