Ruby on rails Rails验证-确认是否存在未保存在模型中的表单属性

Ruby on rails Rails验证-确认是否存在未保存在模型中的表单属性,ruby-on-rails,validation,Ruby On Rails,Validation,我的应用程序有一个带有元素foo的form_for标记,它没有保存在form_for中使用的对象的模型中 我需要使用Rails验证助手确认用户提交了该元素的值。但是,“presence”验证器调用object.foo来确认它有一个值。由于foo未保存为对象的一部分,如何进行此验证 谢谢 试试这个 class SearchController < ApplicationController include ActiveModel::ForbiddenAttributesProtectio

我的应用程序有一个带有元素foo的form_for标记,它没有保存在form_for中使用的对象的模型中

我需要使用Rails验证助手确认用户提交了该元素的值。但是,“presence”验证器调用object.foo来确认它有一个值。由于foo未保存为对象的一部分,如何进行此验证

谢谢

试试这个

class SearchController < ApplicationController
  include ActiveModel::ForbiddenAttributesProtection

  def create
    # Doesn't have to be an ActiveRecord model
    @results = Search.create(search_params)
    respond_with @results
  end

  private

  def search_params
    # This will ensure that you have :start_time and :end_time, but will allow :foo and :bar
    params.require(:val1, :foo).permit(:foo, :bar , whatever else)
  end
end

class Search < ActiveRecord::Base
  validates :presence_of_foo

  private

  def presence_of_foo
    errors.add(:foo, "should be foo") if (foo.empty?) 
  end
end
class SearchController
查看更多信息试试这个

class SearchController < ApplicationController
  include ActiveModel::ForbiddenAttributesProtection

  def create
    # Doesn't have to be an ActiveRecord model
    @results = Search.create(search_params)
    respond_with @results
  end

  private

  def search_params
    # This will ensure that you have :start_time and :end_time, but will allow :foo and :bar
    params.require(:val1, :foo).permit(:foo, :bar , whatever else)
  end
end

class Search < ActiveRecord::Base
  validates :presence_of_foo

  private

  def presence_of_foo
    errors.add(:foo, "should be foo") if (foo.empty?) 
  end
end
class SearchController
查看更多信息试试这个

class SearchController < ApplicationController
  include ActiveModel::ForbiddenAttributesProtection

  def create
    # Doesn't have to be an ActiveRecord model
    @results = Search.create(search_params)
    respond_with @results
  end

  private

  def search_params
    # This will ensure that you have :start_time and :end_time, but will allow :foo and :bar
    params.require(:val1, :foo).permit(:foo, :bar , whatever else)
  end
end

class Search < ActiveRecord::Base
  validates :presence_of_foo

  private

  def presence_of_foo
    errors.add(:foo, "should be foo") if (foo.empty?) 
  end
end
class SearchController
查看更多信息试试这个

class SearchController < ApplicationController
  include ActiveModel::ForbiddenAttributesProtection

  def create
    # Doesn't have to be an ActiveRecord model
    @results = Search.create(search_params)
    respond_with @results
  end

  private

  def search_params
    # This will ensure that you have :start_time and :end_time, but will allow :foo and :bar
    params.require(:val1, :foo).permit(:foo, :bar , whatever else)
  end
end

class Search < ActiveRecord::Base
  validates :presence_of_foo

  private

  def presence_of_foo
    errors.add(:foo, "should be foo") if (foo.empty?) 
  end
end
class SearchController

查看更多信息

您可能应该在控制器操作的
参数
中检查它的存在:

def create
  @model = MyModel.find(params[:id])
  unless params[:foo].present?
    @model.errors.add(:foo, "You need more foo")
  end

  # ...
end
如果
:foo
是未保存在数据库中的对象的属性,并且确实希望使用ActiveRecord验证,则可以为其创建一个
attr\u访问器
,并像这样验证存在性

class MyModel < ActiveRecord::Base
  attr_accessor :foo
  validates :foo, presence: true
end
classmymodel

但这可能会导致保存无效记录,因此您可能不想这样做。

您可能应该在控制器操作的
参数中检查是否存在该记录:

def create
  @model = MyModel.find(params[:id])
  unless params[:foo].present?
    @model.errors.add(:foo, "You need more foo")
  end

  # ...
end
如果
:foo
是未保存在数据库中的对象的属性,并且确实希望使用ActiveRecord验证,则可以为其创建一个
attr\u访问器
,并像这样验证存在性

class MyModel < ActiveRecord::Base
  attr_accessor :foo
  validates :foo, presence: true
end
classmymodel

但这可能会导致保存无效记录,因此您可能不想这样做。

您可能应该在控制器操作的
参数中检查是否存在该记录:

def create
  @model = MyModel.find(params[:id])
  unless params[:foo].present?
    @model.errors.add(:foo, "You need more foo")
  end

  # ...
end
如果
:foo
是未保存在数据库中的对象的属性,并且确实希望使用ActiveRecord验证,则可以为其创建一个
attr\u访问器
,并像这样验证存在性

class MyModel < ActiveRecord::Base
  attr_accessor :foo
  validates :foo, presence: true
end
classmymodel

但这可能会导致保存无效记录,因此您可能不想这样做。

您可能应该在控制器操作的
参数中检查是否存在该记录:

def create
  @model = MyModel.find(params[:id])
  unless params[:foo].present?
    @model.errors.add(:foo, "You need more foo")
  end

  # ...
end
如果
:foo
是未保存在数据库中的对象的属性,并且确实希望使用ActiveRecord验证,则可以为其创建一个
attr\u访问器
,并像这样验证存在性

class MyModel < ActiveRecord::Base
  attr_accessor :foo
  validates :foo, presence: true
end
classmymodel

但这可能会导致保存无效记录,因此,您可能不想这样做。

在将控制器传递给模型之前,您很可能需要检查控制器中参数的内容。在将控制器传递给模型之前,您很可能需要检查控制器中参数的内容。您很可能需要检查控制器中参数的内容在将控制器中的参数传递给模型之前,请先检查控制器中参数的内容,然后再将其传递给模型。