I’m using rails 3.1.0.rc5 while trying to implement a pseudo enum field.
I am trying to validate the value of a field that is bound to a static list. Unfortunately it always fails validation.
model code>
STATUSES = %w( pending active completed )
validates_inclusion_of :status, :in => STATUSES, :on => :create, :message => "%{value} must be one of #{STATUSES.join ', '}"
form markup>
<div class="field">
<%= f.label :status %><br />
<%= select_tag :status, options_for_select(Task::STATUSES, @task.status) %>
</div>
unfortunately the validation routine for status always fails. looking at the post data, it contains a value and it is valid.. so i’m not sure what is going on?
i’ve tried adding the following as well based on some googling
def status=(value)
write_attribute(:status, value.to_s)
end
def status
attributes = attributes_before_type_cast
if attributes["status"]
read_attribute(:status).to_sym
else
nil
end
end
however, the status= (setter) never seems to get called.
Thanks!
I can’t tell without having a look at your controller but i’m assuming your controller is doing something like:
Your form is going to post something like this:
When it should be putting the status in the user hash like so:
You’ll want to alter your form to post the attributes correctly: