characters_controller.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class CharactersController < ApplicationController
  2. before_action :set_character, only: [:show, :edit, :update, :destroy]
  3. # GET /characters
  4. # GET /characters.json
  5. def index
  6. @characters = Character.all
  7. end
  8. # GET /characters/1
  9. # GET /characters/1.json
  10. def show
  11. end
  12. # GET /characters/new
  13. def new
  14. @character = Character.new
  15. end
  16. # GET /characters/1/edit
  17. def edit
  18. end
  19. # POST /characters
  20. # POST /characters.json
  21. def create
  22. @character = Character.new(character_params)
  23. respond_to do |format|
  24. if @character.save
  25. format.html { redirect_to @character, notice: 'Character was successfully created.' }
  26. format.json { render :show, status: :created, location: @character }
  27. else
  28. format.html { render :new }
  29. format.json { render json: @character.errors, status: :unprocessable_entity }
  30. end
  31. end
  32. end
  33. # PATCH/PUT /characters/1
  34. # PATCH/PUT /characters/1.json
  35. def update
  36. respond_to do |format|
  37. if @character.update(character_params)
  38. format.html { redirect_to @character, notice: 'Character was successfully updated.' }
  39. format.json { render :show, status: :ok, location: @character }
  40. else
  41. format.html { render :edit }
  42. format.json { render json: @character.errors, status: :unprocessable_entity }
  43. end
  44. end
  45. end
  46. # DELETE /characters/1
  47. # DELETE /characters/1.json
  48. def destroy
  49. @character.destroy
  50. respond_to do |format|
  51. format.html { redirect_to characters_url, notice: 'Character was successfully destroyed.' }
  52. format.json { head :no_content }
  53. end
  54. end
  55. private
  56. # Use callbacks to share common setup or constraints between actions.
  57. def set_character
  58. @character = Character.find(params[:id])
  59. end
  60. # Never trust parameters from the scary internet, only allow the white list through.
  61. def character_params
  62. params.require(:character).permit(:name, :health, :strength, :intelligence, :agility, :luck, :avatar)
  63. end
  64. end