Explorar el Código

Added validation errors to notice.

master
Nils Dittberner hace 9 años
padre
commit
4854aa19d7
Se han modificado 3 ficheros con 22 adiciones y 8 borrados
  1. +9
    -6
      app/controllers/categories_controller.rb
  2. +11
    -2
      app/models/category.rb
  3. +2
    -0
      app/models/document.rb

+ 9
- 6
app/controllers/categories_controller.rb Ver fichero

@@ -4,7 +4,7 @@ class CategoriesController < ApplicationController
# GET /categories
# GET /categories.json
def index
@categories = Category.all
@categories = User.find(session[:user_id]).categories
end

# GET /categories/1
@@ -30,7 +30,7 @@ class CategoriesController < ApplicationController

respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.html { redirect_to :categories, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
@@ -56,10 +56,13 @@ class CategoriesController < ApplicationController
# DELETE /categories/1
# DELETE /categories/1.json
def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
if @category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
else
redirect_to categories_url, notice: @category.errors.full_messages.first
end
end



+ 11
- 2
app/models/category.rb Ver fichero

@@ -1,10 +1,12 @@
class Category < ApplicationRecord
has_many :documents
has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id', dependent: :destroy
has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id'
belongs_to :parent, class_name: 'Category', optional: true
belongs_to :user

before_destroy :check_for_subcategories
before_destroy :check_for_subcategories, :check_for_documents

validates :name, presence: true

def get_fqcn
fqcn = name
@@ -25,4 +27,11 @@ class Category < ApplicationRecord
throw :abort
end
end

def check_for_documents
unless documents.empty?
errors.add(:base, 'Documents present')
throw :abort
end
end
end

+ 2
- 0
app/models/document.rb Ver fichero

@@ -4,6 +4,8 @@ class Document < ApplicationRecord
has_one :content, dependent: :destroy, required: false
has_and_belongs_to_many :tags

validates :category, :doc, presence: true

has_attached_file :doc,
{
styles: { thumb: ["200x", :png] },


Cargando…
Cancelar
Guardar