Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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 在rails中枚举选择_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 在rails中枚举选择

Ruby on rails 在rails中枚举选择,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在表单中选择枚举值时遇到问题 这是一张表格 =form_for @ticket , remote: true do |f| .errors p = f.label :name, class: 'label_hidden' = f.text_field :name, placeholder:'Input your name', class:'form-control' p = f.label :email,

我在表单中选择枚举值时遇到问题 这是一张表格

  =form_for @ticket , remote: true do |f|
    .errors        
    p  
      = f.label :name, class: 'label_hidden' 
      = f.text_field :name, placeholder:'Input your name', class:'form-control'
    p  
      = f.label :email, class: 'label_hidden' 
      = f.email_field :email, placeholder:'Input your email', class:'form-control'
    p  
      = f.label :department, class: 'label_hidden'
      = f.select :department, Ticket.departments.keys , class:'form-control'       
    p
      = f.submit 'Submit', class:'btn btn-default custom'
在我的数据库中,我有一个字符串类型的字段部门,也有一个模型

class Ticket < ActiveRecord::Base
  enum department: [:issue, :qa, :promotion]
  validates :name, :email, :subject, :body, :department, presence: true
end
classticket

当我发送此表单时-在参数中一切正常(部门参数在那里),并且我已允许控制器中使用此参数。我仍然收到“部门不能为空”。我错在哪里?

我想您将enum作为字符串接收,因此应该使用setter方法转换它:

def department=(val)
    self[:department] = val.to_sym
end

大家好,欢迎来到Stack Overflow。如果您能向我们展示服务器日志中参数通过的部分(以及后面的几行),那么我们就可以看到它们的结构。您是否尝试过在rails控制台中手动创建一个票证,并查看它是否也有相同的错误(或者它是否只是表单)?您好Taryn!!是的,我已经试过了-它一直在退回一笔交易!!有帮助的是我将db中的字符串值改为整数,并且在参数中设置了一个丑陋的类型转换。我相信我不理解rails中的枚举((.在使用枚举时,数据库中枚举的值是否应为distincly integer?我还有一个问题,类:'form controll'根本不影响选择字段,请看一下,好吗?看起来cema sp已经给了您结果…问题是枚举中的值是符号,而不是字符串(类似但不相同)…因此,在将其作为值添加之前,需要将其转换为符号:)