Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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_Ruby On Rails 4_Textfield - Fatal编程技术网

Ruby on rails 必须至少填写一个文本字段

Ruby on rails 必须至少填写一个文本字段,ruby-on-rails,ruby,ruby-on-rails-4,textfield,Ruby On Rails,Ruby,Ruby On Rails 4,Textfield,我的视图中有3个文本字段,在其中输入学生姓名。当然,您可以输入一个学生或三个学生,但我想确保至少提供了一个学生,因为项目必须指定一个学生 以下是我的看法: <%= form_for @project, url: projects_path do |f| %> <p> <%= f.label :name, "Name" %> <%= f.text_field :name %> </p>

我的视图中有3个
文本字段
,在其中输入学生姓名。当然,您可以输入一个学生或三个学生,但我想确保至少提供了一个学生,因为项目必须指定一个学生

以下是我的看法:

<%= form_for @project, url: projects_path do |f| %>
    <p>
        <%= f.label :name, "Name" %>
        <%= f.text_field :name %>
    </p>

    <p>
        <%= f.fields_for :students do |s| %>
            <%= s.label :name %>
            <%= s.text_field :name %>
        <% end %>
    </p>

    <p>
        <%= f.submit "Submit" %>
    </p>
<% end %>
我想实现的是检查是否至少提供了一名学生,如果不只是显示警报或禁用提交

编辑 本项目中使用的模型:

class Student < ActiveRecord::Base
  belongs_to :project
end

class Project < ActiveRecord::Base
  has_many :students
  accepts_nested_attributes_for :students
  validate :validate_student_count

  def validate_student_count
    errors.add(:students, "at least one is required") if students.count < 1
  end
end
class-Student
互联网上有很多类似的问题。这里有一些例子:互联网上有很多非常相似的问题。这里有一些例子:互联网上有很多非常相似的问题。这里有一些例子:互联网上有很多非常相似的问题。下面是一些示例:

只需添加一个自定义验证规则,如下所示:

  validate :validate_student_count

  def validate_student_count
    errors.add(:students, "at least one is required") if students.count < 1
  end
验证:验证学生人数 def验证学生人数 错误。如果students.count<1,则添加(:students,“至少需要一个”) 结束
只需添加自定义验证规则,如下所示:

  validate :validate_student_count

  def validate_student_count
    errors.add(:students, "at least one is required") if students.count < 1
  end
验证:验证学生人数 def验证学生人数 错误。如果students.count<1,则添加(:students,“至少需要一个”) 结束
只需添加自定义验证规则,如下所示:

  validate :validate_student_count

  def validate_student_count
    errors.add(:students, "at least one is required") if students.count < 1
  end
验证:验证学生人数 def验证学生人数 错误。如果students.count<1,则添加(:students,“至少需要一个”) 结束
只需添加自定义验证规则,如下所示:

  validate :validate_student_count

  def validate_student_count
    errors.add(:students, "at least one is required") if students.count < 1
  end
验证:验证学生人数 def验证学生人数 错误。如果students.count<1,则添加(:students,“至少需要一个”) 结束
您没有告诉我们您有什么问题,只是告诉我们您试图实现什么。我只是编辑了我的问题。您没有告诉我们您有什么问题,只是告诉我们您试图实现什么。我只是编辑了我的问题。您没有告诉我们您有什么问题,只有你想达到的目标。我刚刚编辑了我的问题。你没有告诉我们你有什么问题,只有你想达到的目标。我刚刚编辑了我的问题。