anti-k anti-k - 1 year ago 203
Ruby Question

enum select in rails

I've got a problem with selecting enum value in form
here is a form

=form_for @ticket , remote: true do |f|
= f.label :name, class: 'label_hidden'
= f.text_field :name, placeholder:'Input your name', class:'form-control'
= f.label :email, class: 'label_hidden'
= f.email_field :email, placeholder:'Input your email', class:'form-control'
= f.label :department, class: 'label_hidden'
= :department, Ticket.departments.keys , class:'form-control'
= f.submit 'Submit', class:'btn btn-default custom'

In my db I have a field department that is string type, also in a model I have

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

When i send this form - in params everything is ok( the department param is there), also I have permitted this param in controller. Still I recieve "department can't be blank". Where I am wrong?

Answer Source

I suppose that you receive enum as a String, so you should convert it with setter method:

def department=(val)
    self[:department] = val.to_sym
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download