Forum: Ruby on Rails Getting an attr_accessor to work

Ea712fa65258a359b36d3d4e6a71cdbd?d=identicon&s=25 Asa Romberger (Guest)
on 2014-06-22 04:35
(Received via mailing list)
I have a user model with a number of fields representing the user

When a user is first signing up, I have an initial question I want to
that is a one time and not part of the user profile.

In the model, I have:
    attr_accessor :completed_cfdym
    validates(:completed_cfdym, inclusion: { in: ['Yes', 'No'], message:
"%{value} is not a valid response"} )

In the view, I have:

    <% f.label :completed_cfdym, "Have you Completed 'A Conversation for
The Difference You Make'", class: 'span5'  %>
    <%= :completed_cfdym, {'' => nil, 'Yes' => 'Yes', 'No' =>
'No'}, {}, { :class => 'span1' } %><br>

In the controller, in the create method, I have:

    if params[:completed_cfdym] == 'Yes'

Looking at the console model in the development environment, I see that
completed_cfdym is returning nil, 'Yes', or 'No' based on what I select.


The validation fails and, when it returns to the forms page, the
completed_cfdym is not preserved - it always returns to ''

Also, if I remove the validates, the test in the controller does not
to be able to recognize the 'Yes'.

What am I missing?
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.