I have a support form where users can send messages. This support form should have a checkbox and the message sould only be sent if the check box has been activated. So far I have in my support.rb model (just the relevant part):
class Support
include ActiveModel::Validations
attr_accessor :dataprotection
validates :dataprotection, :acceptance => true
end
As far as I understand I don't even need the attr_accessor reference since it creates a virtual attribute if :dataprotection is not part of my model, so after adding :dataprotection I have not done a migration. Anyhow, I do not save the message and the data in a database, it is just sent it to an email address.
The view is (again just the relevant part, I use haml):
= form_for :support, :url => { :action => "create" }, :html => { :method => :post } do |f|
= render 'shared/error_messages_support'
%div
= check_box_tag 'dataprotection'
= label_tag(:dataprotection, simple_format(t"support.dataprotection"))
The form is displayed correctly, also the check box appears and is clickable. But if the user does not check it the form is sent anyhow, there is no error message.
What do I need to change in order to get an error message in case the box is not ticked?
My controller is
class SupportsController < ApplicationController
def new
@support = Support.new(:id => 1) # id is used to deal with form
end
def create
@support = Support.new(params[:support])
if @support.save
flash[:success] = t "support.flashsuccess"
redirect_to(root_path)
else
render 'new'
end
end
end
My Gemfile is:
ruby '2.0.0'
source 'http://rubygems.org'
gem 'rails', '4.0.0'
gem 'jquery-rails', '2.1.1'
gem 'capistrano', '2.14.1'
gem "therubyracer", '~> 0.11.4'
gem 'carrierwave', '0.8.0'
gem 'haml', '~> 4.0'
gem 'mysql2', '0.3.11'
gem 'rmagick', '2.13.2'
gem 'fancybox2-rails', '~> 0.2.8'
gem 'sitemap_generator', '3.4'
gem 'whenever', '0.7.3', :require => false
gem 'will_paginate', '3.0.5'
gem "friendly_id", "~> 5.0.3"
gem 'turbolinks'
gem 'jquery-turbolinks'
gem 'protected_attributes'
gem 'globalize', '~> 4.0.2'
gem 'sass-rails', '~> 4.0.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
group :development do
gem 'rspec-rails', '2.13.2'
gem 'annotate', '~> 2.4.1.beta'
gem 'faker', '0.9.5', :require => false
gem "database_cleaner", "~> 1.0.1"
gem 'debugger'
end
Aucun commentaire:
Enregistrer un commentaire