user2567129
user2567129

Reputation: 91

Adding checkbox in signup form

I have a signup form which needs an checkbox for terms service. I forgot to add that column during the initial scaffold. I do not what do after .Please help me to solve the problem.

Upvotes: 1

Views: 2327

Answers (3)

thecrentist
thecrentist

Reputation: 1271

To add/update to Bachans' answer, in Rails 4 you'd do:

# User Controller
  private
    def user_params
      params.require(:user).permit(:name, :email, :terms_accepted)
    end

instead of

# User model
attr_accessible :terms_accepted

I hope that works!

Upvotes: 0

Debadatt
Debadatt

Reputation: 6015

You can create an attribute acessor for the terms and conditions field in the model. For example

class User < ActiveRecod::Base
attr_accessor :terms_and_conditions
end

and in form

<%=form_for(@user) do |f|%>
<%=f.check_box :terms_and_conditions %>
<% end %>

Or


You can take the help of "acceptance" method of active record. Please check the method in http://guides.rubyonrails.org/active_record_validations.html

This validation is very specific to web applications and this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).

class User < ActiveRecord::Base
  validates :terms_and_conditions, acceptance: true
end

Upvotes: 1

Bachan Smruty
Bachan Smruty

Reputation: 5734

For that you can add a new migration to add a new column as boolean field to your users table. http://guides.rubyonrails.org/migrations.html

rails g migration AddTermsAcceptedToUsers

It will create a migration file in your db/migrate folder. Now you need to add the code to it.

class AddTermsAcceptedToUsers < ActiveRecord::Migration
  def change
    add_column :users, :terms_accepted, :boolean, :default => false
  end
end

Then do rake db:migrate.

Now your users table is having a column as terms_accepted. Then add it as attr_accesible.

attr_accessible :terms_accepted.

Now use this field and show it as checkbox in the registration page.

<%= f.check_box :terms_accepted%>

Upvotes: 2

Related Questions