Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox for terms and conditions, without column in database

I need a "I accept terms of service" checkbox on a page, it has to be checked in order for the order to proceed. It seems hence illogical to have a column in the database to match this (whether user has accepted or declined terms).

I am using the form helper like this in my view:

<%= check_box("client", "terms") %>

And in my model:

validates_acceptance_of :terms

At the moment it is not working at all.

This seems like a really common piece of code, yet I can't find it used anywhere without having the terms in the model. Else I could use javascript to validate it, but would prefer to keep it all the in model.

like image 527
vectran Avatar asked Feb 10 '10 21:02

vectran


3 Answers

This should work fine, without a database column or attr_accessor: http://guides.rubyonrails.org/active_record_validations.html#acceptance

I would be inclined to check your params hash is as it should be i.e. that the 'terms' attribute is being passed within the 'client' hash, perhaps try adding raise params.inspect on your controller create action to help you debug?

like image 176
Paul Groves Avatar answered Nov 06 '22 08:11

Paul Groves


What about having an attr_accessor :terms in your Client model?

like image 38
alex.zherdev Avatar answered Nov 06 '22 07:11

alex.zherdev


I had this working with these settings:

In the controller, I have added :terms_of_service as a permitted field:

def application_params
  params.require(:application).permit(. . . , :terms_of_service)
end

In the model:

attr_accessor :terms_of_service
validates :terms_of_service, :acceptance => true

In the view:

<%= f.check_box("terms_of_service", :checked => false) %>
like image 3
Roozbeh Zabihollahi Avatar answered Nov 06 '22 06:11

Roozbeh Zabihollahi