Tuesday, June 3, 2008

You better accept it!

Time after time you will find all kind of term of services which ask you to click 'I Agree'

Well, you better agree. Otherwise, you will be kicked out!

Here is the rails way to enforce this:

Model side:


class User < ActiveRecord::BASE
validates_acceptance_of :term_of_service
...
attr_accessible :term_of_service
end


RHTML side:


<%form_for :user, @user, :url => users_path do f %>
<p>
... other controls ...
</p>
<p>
<%= f.check_box :terms_of_service %>
I accept the terms of service.
</p> <p>
<%= submit_tag "Save" %>
</p>
<% end %>

The :term_of_service is actually virtual. It has not direct value in database. ActiveRecord temperately hold it in memory.

No comments: