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 :
On Sunday, June 8, 2014 5:40:20 PM UTC-4, Asa Romberger wrote:
>
> 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
>
> On Sunday, June 8, 2014 6:39:34 AM UTC-7, James Kwame wrote:
>>
>> What exactly are you trying to achieve with this approach, why do you 
>> need to have the second attr not in the database and validated? 
>>
>> To answer your question, yes you can force it to be a boolean, by 
>> creating your own accessor methods
>>
>> Instead of attr_accessor :attrvalue
>>   def attrvalue
>>     @value
>>   end
>>
>>   def attrvalue=(new_value)
>>       if new_value == "true"
>>          @value = true
>>       else
>>          @value = false
>>       end
>>   end
>>
>> On Saturday, June 7, 2014 9:17:08 PM UTC-4, Asa Romberger wrote:
>>>
>>> I have a model with a boolean variable in the database and one added by 
>>> attr_accessor:
>>>
>>> In the model:
>>>
>>>   attr_accessor :attrvalue
>>>
>>>   validates(:dbvalue, inclusion: { in: [true, false], message: "%{value} 
>>> is not a valid response"} )
>>>
>>>   validates(:attrvalue, inclusion: { in: [true, false], message: 
>>> "%{value} is not a valid response"} )
>>>
>>> In the view:
>>>
>>>     <%= form_for(@user) do |f| %>
>>>
>>>       <%= f.select :dbvalue, {'' => nil, 'Yes' => true, 'No' => false}, 
>>> {},  { :class => 'span1' } %>
>>>
>>>       <%= f.select :attrvalue, {'' => nil, 'Yes' => true, 'No' => 
>>> false}, {},  { :class => 'span1' } %>
>>>
>>>     <% end %>
>>>
>>> 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?
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/3aca2f00-e13b-4f83-882d-f34083f9250d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to