Question

Am new to rails and I have an application that allows the user to create a deadline much like creating a post for a blog and then want to be able to create a short url for that deadline to be shared by the user? How would i go about creating a short url like bit.ly.

  class DeadlinesController < ApplicationController

def new
    @deadline = current_user.deadlines.new
end

def create
    @deadline = current_user.deadlines.new(params[:deadline].permit(:title, :date, :description))
    if @deadline.save
        redirect_to @deadline
    else
        render 'new'
    end
end

def show
    @deadline = Deadline.find(params[:id])
end

def edit
    @deadline = current_user.deadlines.find(params[:id])
end

def index

    @deadlines = current_user.deadlines.all
    @deadlines = current_user.deadlines.paginate(:page => params[:page], :per_page => 5)
end

def update
    @deadline = current_user.deadlines.find(params[:id])

    if @deadline.update(params[:deadline].permit(:title, :date, :description))
        redirect_to @deadline
    else
        render 'edit'
    end
end

def destroy
    @deadline = current_user.deadlines.find(params[:id])
    @deadline.destroy

    redirect_to deadlines_path

end

private
def post_params
    params.require(:deadline).permit(:title, :date, :description)
end

end

Deadlines model:

   class Deadline < ActiveRecord::Base

    validates :title, presence: true,
                length: { minimum: 8 }


    validates :date, presence: true

    validates :description, presence: true

   #validates_format_of :date, :with => /\A[0-9]{4}-[0-1][0-9]-[0-3][0-9]\z/, :message => "Enter Date in this format: YYYY-MM-DD" 
    belongs_to :user    

end
Was it helpful?

Solution

You could use shortener gem which makes it easy to create shortened URLs for Rails Application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top