class MicropostsController < ApplicationController
  before_action :set_micropost, only: %i[ show edit update destroy ]
  def create
    @micropost = Micropost.new(micropost_params)
    authorize @micropost
    if @micropost.save
      flash[:success] = "Post created!"
      redirect_to request.referrer || root_url
    else
      flash[:error] = "Post failed!"
      redirect_to request.referrer || root_url
    end
  end

  def destroy
    authorize @micropost
    @micropost.destroy
    respond_to do |format|
      format.html { redirect_to request.referrer || root_url, notice: "Post was deleted." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_micropost
      @micropost = Micropost.find(params[:id])
    end

    def micropost_params
      params.require(:micropost).permit(:content, :micropostable_type, :micropostable_id)
    end
end
