view app/controllers/pastes_controller.rb @ 64:04164da6c12c

Added document level character encoding as per validator's suggestion.
author Edho Arief <edho@myconan.net>
date Thu, 04 Oct 2012 03:31:57 -0700
parents 817c54a72b58
children f7b7a39fb48c
line wrap: on
line source

class PastesController < ApplicationController
  caches_page :show
  # GET /pastes/1
  # GET /pastes/1.json
  def show
    id = params[:id].to_i
    if id.to_s != params[:id]
      redirect_to paste_path(id)
      return
    end
    @paste = Paste.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.txt # show.html.erb
    end
  end

  # GET /pastes/new
  # GET /pastes/new.json
  def new
    @paste = Paste.new
    begin
      @paste.paste = Paste.find(params[:base]).paste
    rescue
    end

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @paste }
    end
  end

  # POST /pastes
  # POST /pastes.json
  def create
    @paste = Paste.new
    @paste.paste = params[:paste][:paste]
    @paste.ip = request.remote_ip

    begin
      respond_to do |format|
        if @paste.save
          format.html { redirect_to @paste, :notice => 'Paste was successfully created.' }
          format.json { render :json => @paste, :status => :created, :location => @paste }
        else
          format.html { render :action => "new" }
          format.json { render :json => @paste.errors, :status => :unprocessable_entity }
        end
      end
    rescue ActiveRecord::RecordNotUnique
      redirect_to paste_path(Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first)
    end
  end

end