diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index e251d92..daf6bc9 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -30,6 +30,9 @@ def update end def destroy + if @category.destroy + redirect_to categories_path + end end def index diff --git a/app/controllers/expenses_controller.rb b/app/controllers/expenses_controller.rb index 1bca22f..372fe2e 100644 --- a/app/controllers/expenses_controller.rb +++ b/app/controllers/expenses_controller.rb @@ -13,7 +13,7 @@ def create redirect_to expenses_path flash[:success] = "New expence has been created" else - render Expense.new + redirect_to new_expense_path end end diff --git a/app/models/category.rb b/app/models/category.rb index db5b988..bf5b0e6 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,4 +1,7 @@ class Category < ActiveRecord::Base belongs_to :user has_many :expenses + + validates :name , uniqueness: true + validates :name , presence: true end diff --git a/app/models/expense.rb b/app/models/expense.rb index 3975f17..057aca7 100644 --- a/app/models/expense.rb +++ b/app/models/expense.rb @@ -1,4 +1,6 @@ class Expense < ActiveRecord::Base belongs_to :category belongs_to :user + + validates :amount, :category, :date, :description, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 79709a5..bd0cb96 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,4 +6,24 @@ class User < ActiveRecord::Base has_many :categories has_many :expenses, through: :categories + + after_create :create_default_categories + + DEFAULT_CATEGORIES = [ + {name: 'Phone bill'}, + {name: 'Entertainment'}, + {name: 'Rent'}, + {name: 'Travel'}, + {name: 'Parties'}, + {name: 'Gifts'}, + {name: 'Phone bill'}, + {name: 'Miscellaneous'} + ] + + def create_default_categories + DEFAULT_CATEGORIES.each do |default_attrs| + self.categories.build(default_attrs) + end + end + end diff --git a/app/views/expenses/_form.html.erb b/app/views/expenses/_form.html.erb index 6709233..9c82808 100644 --- a/app/views/expenses/_form.html.erb +++ b/app/views/expenses/_form.html.erb @@ -27,9 +27,10 @@
Manage all your expences with hassle free
+Manage all your expences with hassle free
\ No newline at end of file +