Display validation errors to user

This commit is contained in:
Lukas Matt 2014-03-24 11:16:14 -04:00
parent 6309e1a4ee
commit 512d96bda6
2 changed files with 4 additions and 3 deletions

View file

@ -27,7 +27,8 @@ class ReportController < ApplicationController
end
def create
if current_user.reports.create! report_params
report = current_user.reports.new(report_params)
if report.save
flash.now[:notice] = I18n.t 'report.status.created'
render :nothing => true, :status => 200
else

View file

@ -4,7 +4,7 @@ class Report < ActiveRecord::Base
validates :post_type, presence: true
validates :text, presence: true
validate :entry_exists, :on => :create
validate :entry_does_not_exist, :on => :create
belongs_to :user
belongs_to :post
@ -12,7 +12,7 @@ class Report < ActiveRecord::Base
after_commit :send_report_notification, :on => :create
def entry_exists
def entry_does_not_exist
if Report.where(post_id: post_id, post_type: post_type).exists?(user_id: user_id)
errors[:base] << 'You cannot report the same post twice.'
end