On Sunday, June 8, 2014 2:17:08 AM UTC+1, Asa Romberger wrote:
attr_accessor :attrvalue
The dbvalue works, the attrvalue does not work and always throws the message
attrvalue is not a valid response.
I assume that attrvalue is not a boolean. Can I force it to be a boolean?
Alternately, is there another way to handle it?
Forms always submit strings, so the value that ends up in your model is
the string “true” rather than he boolean true. For the dbvalue the
accessors activerecord provides know that the corresponding column is a
boolean column so they convert the string to the appropriate boolean
(see
for some of the typecasting )
You could either choose to do the same (ie provide your own setter
method that does this conversion) or choose to mangle the parameters in
the controller. I find both options a little icky - the model is the one
that knows which attribute should be of each type but it feels a little
dirty that a detail of the way in which http form posts are handled
(values are always strings) ends up in your model.
The second attr is a transient only used when the user signs up for an
account, so I did not want to save it it in the database.
I now have in the model:
attr_accessor :completed_cfdym
def completed_cfdym @completed_cfdym
end
def completed_cfdym=(var)
if var = ‘true’ @completed_cfdym = ‘true’
else
if var = ‘false’ @completed_cfdym = ‘false’
else @completed_cfdym = ‘’
end
end
end
validates(:completed_cfdym, inclusion: { in: [true, false], message:
“%{value} is not a valid response”} )
The ‘’ is because I do want force the user to answer.
And the view:
<%= f.label :completed_cfdym, "Have you already completed 'A
Conversation for the Difference You Make in Life?", class: ‘span5’ %>
<%= f.select :completed_cfdym, {’’ => nil, ‘Yes’ => true, ‘No’ =>
false}, {}, { :class => ‘span1’ } %>
This still fails with:
Completed cfdym is not a valid response
It also does not preserve the value that I set when it displays the
error
and goes back to the ‘’ => ‘nil’ setting
You should remove the quotes from your boolean values. @completed_cfdym = ‘true’ should be @completed_cfdym = true this
should solve the Completed cfdym is not a valid response error.
To preserve form values after failure you have to use params[:fieldname]
<%= f.select :completed_cfdym, params[:completed_dfdym],{’’ => nil,
‘Yes’
=> true, ‘No’ => false}, {}, { :class => ‘span1’ } %>
Then use render in your controller on form submit failure.
attr_accessor :completed_cfdym
def completed_cfdym
@completed_cfdym
end
def completed_cfdym=(var)
if var == 'true'
@completed_cfdym = true
else
if var == 'false'
@completed_cfdym = false
else
@completed_cfdym = ''
end
end
end
validates(:completed_cfdym, inclusion: { in: [true, false],
message: “%{value} is not a valid response”} )
view:
<%= f.label :completed_cfdym, "Have you already completed 'A
Conversation for the Difference You Make in Life?", class: ‘span5’ %>
<%= f.select :completed_cfdym, {’’ => nil, ‘Yes’ => true, ‘No’ =>
false}, {}, { :class => ‘span1’ } %>
I get the diagnostic:
Completed cfdym is not a valid response
Is there a construct, like the %{value} that I can use to print out the
response?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.