feat: ✨ Generate short url view
stimulus links controller, tailwindcss setup, links controller post route, tests updated Generate short links view
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class LinksController < ApplicationController
|
||||
def home
|
||||
render 'links/home'
|
||||
end
|
||||
def redirect
|
||||
@link = Link.find_by_slug(params[:slug])
|
||||
|
||||
def show
|
||||
@link = Link.find_by_slug(link_params[:slug])
|
||||
|
||||
if @link.nil?
|
||||
render file: "#{Rails.root}/public/404", status: :not_found
|
||||
else
|
||||
@@ -16,7 +12,17 @@ class LinksController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
@link = Link.find_or_create_by(url: link_params[:url])
|
||||
|
||||
if @link.errors.any?
|
||||
render json: @link.errors, status: :unprocessable_entity
|
||||
else
|
||||
render partial: 'links/show', locals: { link: @link }, status: :ok
|
||||
end
|
||||
end
|
||||
|
||||
def link_params
|
||||
params.permit(:slug)
|
||||
params.require(:link).permit(:url)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user