class ChattingsController < ApplicationController
  def show
    @chatting = Chatting.find_by(slug: params[:slug])
    @message = Message.new
  end

  def index
    @chatting = Chatting.new
    @chattings = Chatting.all
  end


  def create
    @chatting = Chatting.new(chatting_params)
    if @chatting.save
      respond_to do |format|
        format.html { redirect_to @chatting }
        format.js
      end
    else
      respond_to do |format|
        flash[:notice] = {error: ["a Chatting with this topic already exists"]}
        format.html { redirect_to new_chatting_path }
        format.js
      end
    end
  end
  
  def new
    if request.referrer.split("/").last == "chattings"
      flash[:notice] = nil
    end
    @chatting = Chatting.new
  end

  private
  def chatting_params
    params.require(:chatting).permit(:topic)
  end
end
