|
| 1 | +class TodosController < ApplicationController |
| 2 | + before_action :set_todo, only: [:show, :edit, :update, :destroy] |
| 3 | + |
| 4 | + # GET /todos |
| 5 | + # GET /todos.json |
| 6 | + def index |
| 7 | + @todos = Todo.all |
| 8 | + @todo = Todo.new |
| 9 | + end |
| 10 | + |
| 11 | + # GET /todos/1 |
| 12 | + # GET /todos/1.json |
| 13 | + def show |
| 14 | + end |
| 15 | + |
| 16 | + # GET /todos/new |
| 17 | + def new |
| 18 | + @todo = Todo.new |
| 19 | + end |
| 20 | + |
| 21 | + # GET /todos/1/edit |
| 22 | + def edit |
| 23 | + end |
| 24 | + |
| 25 | + # POST /todos |
| 26 | + # POST /todos.json |
| 27 | + def create |
| 28 | + @todo = Todo.new(todo_params) |
| 29 | + |
| 30 | + respond_to do |format| |
| 31 | + if @todo.content == "" |
| 32 | + format.html { redirect_to todos_path, notice: 'Todo was blank!'} |
| 33 | + format.json { render json: { error: 'Blank Todo!' }, status: :unprocessable_entity } |
| 34 | + elsif @todo.save |
| 35 | + format.html { redirect_to todos_path } |
| 36 | + format.json { render :show, status: :created, location: @todo } |
| 37 | + else |
| 38 | + format.html { render todos_path } |
| 39 | + format.json { render json: @todo.errors, status: :unprocessable_entity } |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + # PATCH/PUT /todos/1 |
| 45 | + # PATCH/PUT /todos/1.json |
| 46 | + def update |
| 47 | + respond_to do |format| |
| 48 | + if @todo.update(todo_params) |
| 49 | + format.html { redirect_to todos_path } |
| 50 | + format.json { render :show, status: :ok, location: @todo } |
| 51 | + else |
| 52 | + format.html { render :edit } |
| 53 | + format.json { render json: @todo.errors, status: :unprocessable_entity } |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + # DELETE /todos/1 |
| 59 | + # DELETE /todos/1.json |
| 60 | + def destroy |
| 61 | + @todo.destroy |
| 62 | + respond_to do |format| |
| 63 | + format.html { redirect_to todos_url, notice: 'Todo was successfully destroyed.' } |
| 64 | + format.json { head :no_content } |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + private |
| 69 | + # Use callbacks to share common setup or constraints between actions. |
| 70 | + def set_todo |
| 71 | + @todo = Todo.find(params[:id]) |
| 72 | + end |
| 73 | + |
| 74 | + # Never trust parameters from the scary internet, only allow the white list through. |
| 75 | + def todo_params |
| 76 | + params.require(:todo).permit(:content) |
| 77 | + end |
| 78 | +end |
0 commit comments