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