Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 自引用关联时如何验证子对象属性?_Ruby On Rails_Ruby_Validation - Fatal编程技术网

Ruby on rails 自引用关联时如何验证子对象属性?

Ruby on rails 自引用关联时如何验证子对象属性?,ruby-on-rails,ruby,validation,Ruby On Rails,Ruby,Validation,我有一个模型“任务”,有两个实体——“任务”和“子任务”,具有自参考关联 class Task < ActiveRecord::Base has_many :subtasks, class_name: 'Task', foreign_key: 'parent_id', dependent: :destroy belongs_to :parent, class_name: 'Task' accepts_nested_attributes_for :subtasks, al

我有一个模型“任务”,有两个实体——“任务”和“子任务”,具有自参考关联

class Task < ActiveRecord::Base
  has_many    :subtasks, class_name: 'Task', foreign_key: 'parent_id', dependent: :destroy
  belongs_to  :parent, class_name: 'Task'
  accepts_nested_attributes_for :subtasks, allow_destroy: true
  validates :title, presence: true, length: { minimum: 3 }
  validates :priority, presence: true, numericality: { only_integer: true }, length: { is: 1 }
  validates_associated :subtasks
end
现在在TaskShow页面上,我可以创建具有有效属性的子任务,也不能创建具有无效属性的子任务,但我没有收到验证消息。 我怎样才能修好它? 对不起,我的英语不好

PS: 我不知道为什么,但错误存在于控制器中,而不存在于视图中

@project.update_attributes(project_params)
puts @project.errors.full_messages
if @project.errors.empty? || :tasks_attributes?
  redirect_to @project
  puts @project.errors.full_messages

   (0.0ms)  begin transaction
   (0.0ms)  rollback transaction
Tasks title can't be blank
Tasks title is too short (minimum is 3 characters)
Redirected to http://localhost:3000/projects/3
Tasks title can't be blank
Tasks title is too short (minimum is 3 characters)
Completed 302 Found in 6ms (ActiveRecord: 0.2ms)

还应将错误消息添加到视图中:

= simple_form_for @task do |t|
= t.simple_fields_for :subtasks, @task.subtasks.build do |f|
   #error message added here
   - if @task.subtasks.errors.any?
     %ul.errors
       - @task.subtasks.errors.full_messages.each do |msg|
       %li= msg
  .form-inputs
    = f.input :title
    = f.hidden_field :priority, value: @task.priority
  .form-actions
    = f.button  :submit, "Add a subtask"
编辑

您的应用程序中有一个_formpartial,请将该代码更改为

= simple_form_for @task do |f|
- if @task.errors.any?
 ul.errors
   - @task.errors.full_messages.each do |msg|
    => msg

= f.input :title
= f.input :description
= f.input :scheduled
= f.input :deadline
= f.input :priority, collection: [["None", 0], ["High", 3], ["Medium", 2], ["Low", 1]], selected: ["None"]
= f.button :submit

嗯,我得到了#的
未定义方法“errors”子任务模型中是否有
属于:task
?我只有一个模型-'task':
有许多子任务,类名称:'task',外键:'parent\u id',dependent::destroy
属于:parent,类名称:'task'
是,我在布局中有它:`-flash.each do | name,msg |=content | tag:div,msg,class:“警报信息”=yield`该死,不能一个字符串一个字符串地编写代码。如果此处的帮助完整代码仅用于显示闪存消息,则答案中的代码用于显示验证错误消息
= simple_form_for @task do |t|
= t.simple_fields_for :subtasks, @task.subtasks.build do |f|
   #error message added here
   - if @task.subtasks.errors.any?
     %ul.errors
       - @task.subtasks.errors.full_messages.each do |msg|
       %li= msg
  .form-inputs
    = f.input :title
    = f.hidden_field :priority, value: @task.priority
  .form-actions
    = f.button  :submit, "Add a subtask"
= simple_form_for @task do |f|
- if @task.errors.any?
 ul.errors
   - @task.errors.full_messages.each do |msg|
    => msg

= f.input :title
= f.input :description
= f.input :scheduled
= f.input :deadline
= f.input :priority, collection: [["None", 0], ["High", 3], ["Medium", 2], ["Low", 1]], selected: ["None"]
= f.button :submit