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
Ruby on rails 使用ActiveRecord选择输入时出现验证错误_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 使用ActiveRecord选择输入时出现验证错误

Ruby on rails 使用ActiveRecord选择输入时出现验证错误,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有一个用于创建客户机的表单,在其中一个字段中,我必须选择客户机的语言。在模型中,我有一个验证来检查字段是否为null,但即使提供了语言,也会显示验证错误 视图: 控制器: def new end def edit end def create if @client.save redirect_to @client, notice: t_notice('controllers.successfully_created', Client) els

我有一个用于创建客户机的表单,在其中一个字段中,我必须选择客户机的语言。在模型中,我有一个验证来检查字段是否为null,但即使提供了语言,也会显示验证错误

视图:

控制器:

  def new
  end

  def edit
  end

  def create
    if @client.save
      redirect_to @client, notice: t_notice('controllers.successfully_created', Client)
    else
      render action: "new"
    end
  end

  def update
    if @client.update_attributes(params[:client])
      redirect_to @client, notice: t_notice('controllers.successfully_updated', Client)
    else
      render action: "edit"
    end
  end
我使用浏览器的开发工具来检查实际发送的值,尽管模型验证失败

知道发生了什么事吗

编辑

我注意到这个错误只发生在创建新客户机时,而不是在编辑现有客户机时。然而,当我编辑一个客户端时,这个新值并没有被持久化到数据库中

编辑2

  • 使用rails 3.2.22
  • 使用ruby 2.1.6
编辑3


这很奇怪,因为在相同的形式中,我有一些其他select输入正常工作,并且以类似的方式处理。

问题是我使用Globalize添加了一个本地化字段,但我没有考虑:

def create
  @client = Client.new(params[:client])
  if @client.save
    redirect_to @client, notice: t_notice('controllers.successfully_created', Client)
  else
    render action: "new"
  end
end


def update
  @client = Client.find(params[:id])
  if @client.update_attributes(params[:client])
    redirect_to @client, notice: t_notice('controllers.successfully_updated', Client)
  else
    render action: "edit"
  end
end
因为globalize使用:locale键在 批量分配时,应避免在 父模型


我为解决此问题所做的是将我的
:locale
属性重命名为
:client locale
,并执行相应的迁移以重命名列。

是否允许通过控制器中的
strong\u参数进行重命名?我们没有使用strong\u参数,虽然此字段包含在attr_accessible中,但我已添加了所涉及的方法;)这不是我的全部代码,但由于公司隐私,我无法发布整个控制器。。。我无法理解的是,其中一个字段工作正常,而另一个类似的字段从未更新过savedI,您应该检查
@client
来自何处?抱歉,我不知道最后一条评论的意思您应该先找到@client变量,与我的代码进行比较
  def new
  end

  def edit
  end

  def create
    if @client.save
      redirect_to @client, notice: t_notice('controllers.successfully_created', Client)
    else
      render action: "new"
    end
  end

  def update
    if @client.update_attributes(params[:client])
      redirect_to @client, notice: t_notice('controllers.successfully_updated', Client)
    else
      render action: "edit"
    end
  end
def create
  @client = Client.new(params[:client])
  if @client.save
    redirect_to @client, notice: t_notice('controllers.successfully_created', Client)
  else
    render action: "new"
  end
end


def update
  @client = Client.find(params[:id])
  if @client.update_attributes(params[:client])
    redirect_to @client, notice: t_notice('controllers.successfully_updated', Client)
  else
    render action: "edit"
  end
end