You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

77 lines
2.1 KiB

  1. class CategoriesController < ApplicationController
  2. before_action :set_category, only: [:show, :edit, :update, :destroy]
  3. # GET /categories
  4. # GET /categories.json
  5. def index
  6. @categories = Category.all
  7. end
  8. # GET /categories/1
  9. # GET /categories/1.json
  10. def show
  11. end
  12. # GET /categories/new
  13. def new
  14. @category = Category.new
  15. @category.user = User.find(session[:user_id]) # TODO: fuer das setzen der moeglichen subcategories
  16. end
  17. # GET /categories/1/edit
  18. def edit
  19. end
  20. # POST /categories
  21. # POST /categories.json
  22. def create
  23. @category = Category.new(category_params)
  24. @category.user = User.find(session[:user_id]) # TODO: wird ja beim setzen der subcategories nicht in das form geschrieben!
  25. respond_to do |format|
  26. if @category.save
  27. format.html { redirect_to @category, notice: 'Category was successfully created.' }
  28. format.json { render :show, status: :created, location: @category }
  29. else
  30. format.html { render :new }
  31. format.json { render json: @category.errors, status: :unprocessable_entity }
  32. end
  33. end
  34. end
  35. # PATCH/PUT /categories/1
  36. # PATCH/PUT /categories/1.json
  37. def update
  38. respond_to do |format|
  39. if @category.update(category_params)
  40. format.html { redirect_to @category, notice: 'Category was successfully updated.' }
  41. format.json { render :show, status: :ok, location: @category }
  42. else
  43. format.html { render :edit }
  44. format.json { render json: @category.errors, status: :unprocessable_entity }
  45. end
  46. end
  47. end
  48. # DELETE /categories/1
  49. # DELETE /categories/1.json
  50. def destroy
  51. @category.destroy
  52. respond_to do |format|
  53. format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
  54. format.json { head :no_content }
  55. end
  56. end
  57. private
  58. # Use callbacks to share common setup or constraints between actions.
  59. def set_category
  60. @category = Category.find(params[:id])
  61. end
  62. # Never trust parameters from the scary internet, only allow the white list through.
  63. def category_params
  64. params.require(:category).permit(:name, :parent_id)
  65. end
  66. end