Forum: Ruby on Rails Checkboxes returning true or false

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jordan E. (Guest)
on 2007-03-30 18:28
(Received via mailing list)
Hi,
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" %>
  </form>

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
  end

Thanks,
Jord
This topic is locked and can not be replied to.