浏览代码

Added validation errors to notice.

master
父节点
当前提交
4854aa19d7
共有 3 个文件被更改,包括 22 次插入8 次删除
  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 查看文件

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


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


respond_to do |format| respond_to do |format|
if @category.save 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 } format.json { render :show, status: :created, location: @category }
else else
format.html { render :new } format.html { render :new }
@@ -56,10 +56,13 @@ class CategoriesController < ApplicationController
# DELETE /categories/1 # DELETE /categories/1
# DELETE /categories/1.json # DELETE /categories/1.json
def destroy 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
end end




+ 11
- 2
app/models/category.rb 查看文件

@@ -1,10 +1,12 @@
class Category < ApplicationRecord class Category < ApplicationRecord
has_many :documents 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 :parent, class_name: 'Category', optional: true
belongs_to :user belongs_to :user


before_destroy :check_for_subcategories
before_destroy :check_for_subcategories, :check_for_documents

validates :name, presence: true


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

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

+ 2
- 0
app/models/document.rb 查看文件

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


validates :category, :doc, presence: true

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


正在加载...
取消
保存