diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 95f998c..180c22e 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,15 +1,21 @@ class CommentsController < ApplicationController before_action :authenticate_user! + before_action :set_post def create - post = Post.find(params[:post_id]) comments_params = params.require(:comment).permit(:message) comments_params[:user_id] = current_user.id - comment = post.comments.build(comments_params) + comment = @post.comments.build(comments_params) if comment.save - redirect_to post, notice: t('.success') + redirect_to @post, notice: t('.success') else - redirect_to post, alert: t('.error') + redirect_to @post, alert: t('.error') end end end + +private + +def set_post + @post = Post.friendly.find(params[:post_id]) +end diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index 6029ca5..fec2e33 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -25,7 +25,7 @@ def show; end private def set_invitation - @invitation = Invitation.find(params[:id]) + @invitation = Invitation.friendly.find(params[:id]) end def authorize! diff --git a/app/controllers/posts/likes_controller.rb b/app/controllers/posts/likes_controller.rb index b0024a0..7175121 100644 --- a/app/controllers/posts/likes_controller.rb +++ b/app/controllers/posts/likes_controller.rb @@ -5,7 +5,7 @@ class LikesController < LikesController private def set_likeable - @likeable = Post.find(params[:post_id]) + @likeable = Post.friendly.find(params[:post_id]) @post = @likeable end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 7adfc0c..69df32f 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -68,7 +68,7 @@ def post_params end def set_post - @post = Post.find(params[:id]) + @post = Post.friendly.find(params[:id]) end def authorize! diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index d3f433d..ea5b8d2 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -49,7 +49,7 @@ def remove_profile def set_reportable_for_new reportable_id = params[:reportable] reportable_type = params[:reportable_type] - @reportable = Post.find(reportable_id) if reportable_type == 'Post' + @reportable = Post.friendly.find(reportable_id) if reportable_type == 'Post' @reportable = Profile.friendly.find(reportable_id) if reportable_type == 'Profile' @reportable = Comment.find(reportable_id) if reportable_type == 'Comment' @reportable = Reply.find(reportable_id) if reportable_type == 'Reply' diff --git a/app/models/invitation.rb b/app/models/invitation.rb index b5a492c..7cf0a4d 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -13,6 +13,9 @@ class Invitation < ApplicationRecord after_create :create_notification after_create :validate_and_approve_pending_request + extend FriendlyId + friendly_id :project_title, use: :slugged + def set_status self.status = 'pending' end diff --git a/app/models/post.rb b/app/models/post.rb index 51134ac..a3ea31c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -20,6 +20,9 @@ class Post < ApplicationRecord has_rich_text :content + extend FriendlyId + friendly_id :title, use: :slugged + def self.get_sample(amount) published.sample amount end diff --git a/db/migrate/20240215171014_add_slug_to_posts.rb b/db/migrate/20240215171014_add_slug_to_posts.rb new file mode 100644 index 0000000..8b0745c --- /dev/null +++ b/db/migrate/20240215171014_add_slug_to_posts.rb @@ -0,0 +1,6 @@ +class AddSlugToPosts < ActiveRecord::Migration[7.1] + def change + add_column :posts, :slug, :string + add_index :posts, :slug, unique: true + end +end diff --git a/db/migrate/20240215173337_add_slug_to_invitations.rb b/db/migrate/20240215173337_add_slug_to_invitations.rb new file mode 100644 index 0000000..ee67bf3 --- /dev/null +++ b/db/migrate/20240215173337_add_slug_to_invitations.rb @@ -0,0 +1,6 @@ +class AddSlugToInvitations < ActiveRecord::Migration[7.1] + def change + add_column :invitations, :slug, :string + add_index :invitations, :slug, unique: true + end +end diff --git a/db/migrate/20240215115707_create_friendly_id_slugs.rb b/db/migrate/20240215181135_create_friendly_id_slugs.rb similarity index 100% rename from db/migrate/20240215115707_create_friendly_id_slugs.rb rename to db/migrate/20240215181135_create_friendly_id_slugs.rb diff --git a/db/schema.rb b/db/schema.rb index 7712822..5aa8144 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_15_115707) do +ActiveRecord::Schema[7.1].define(version: 2024_02_15_181135) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -119,7 +119,9 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "project_id", null: false + t.string "slug" t.index ["profile_id"], name: "index_invitations_on_profile_id" + t.index ["slug"], name: "index_invitations_on_slug", unique: true end create_table "job_categories", force: :cascade do |t| @@ -177,6 +179,8 @@ t.integer "status", default: 0 t.datetime "published_at" t.string "old_status" + t.string "slug" + t.index ["slug"], name: "index_posts_on_slug", unique: true t.index ["user_id"], name: "index_posts_on_user_id" end