Forum: Ruby on Rails Checkboxes returning true or false

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jordan E. (Guest)
on 2007-03-30 18:28
(Received via mailing list)
I've got a checkbox on my form that the user checks to say whether
they want to receive a newsletter or not. It looks like this:

  <form method="post" action="">
    <%= check_box :customer, :subscribe_to_newsletter %>
    <%= submit_tag "Create Customer" %>

The subscribe_to_newsletter attribute is not in the database, just a
attr_accessor :subscribe_to_newsletter.

I need to write a method to show whether this has been checked or not.
I believe that if the attribute was in the database, then a question
mark method such as subscribe_to_newsletter? would automatically
written for me to return true or false. But, it's not in the database.

I've written this so far, which works, but is there a better way which
I am missing?

  def receive_newsletter?
    subscribe_to_newsletter == "1" ? true : false

This topic is locked and can not be replied to.