Complete CRUD actions for microposts

This commit is contained in:
Trevor Vallender 2023-10-05 19:05:53 +01:00
parent 2014730610
commit 8759a9062c
6 changed files with 37 additions and 2 deletions

View File

@ -2,7 +2,7 @@
class MicropostsController < ApplicationController class MicropostsController < ApplicationController
skip_before_action :require_login, only: [:index, :show] skip_before_action :require_login, only: [:index, :show]
before_action :set_micropost, only: [:show] before_action :set_micropost, only: [:show, :edit, :update, :destroy]
def index def index
@microposts = Micropost.all.order(created_at: :desc) @microposts = Micropost.all.order(created_at: :desc)
@ -24,6 +24,24 @@ class MicropostsController < ApplicationController
def show; end def show; end
def edit; end
def update
if @micropost.update(micropost_params)
redirect_to @micropost, notice: t(".updated")
else
render :edit, status: :unprocessable_entity
end
end
def destroy
if @micropost.destroy
redirect_to microposts_path, notice: t(".deleted")
else
render :edit, status: :unprocessable_entity
end
end
private private
def micropost_params def micropost_params

View File

@ -1,6 +1,7 @@
<div class="micropost"> <div class="micropost">
<%= micropost.content %> <%= micropost.content %>
<div class="created_at"> <div class="created_at">
<%= link_to "Edit", edit_micropost_path(micropost) if micropost.user == current_user %>
<%= link_to micropost.created_at.strftime("%Y-%m-%d %H:%M"), micropost %> <%= link_to micropost.created_at.strftime("%Y-%m-%d %H:%M"), micropost %>
</div> </div>
</div> </div>

View File

@ -0,0 +1,10 @@
<% @title = t(".edit") %>
<%= render partial: "form",
locals: {
user: @user,
button_text: t(".edit"),
title: t(".edit"),
} %>
<%= link_to t(".delete"), @micropost, data: { turbo_method: :delete, turbo_confirm: t(".confirm") } %>

View File

@ -1,3 +1,5 @@
en: en:
microposts: microposts:
created: Successfully created μpost created: Successfully created μpost
updated: Successfully updated μpost
deleted: Successfully deleted μpost

View File

@ -6,4 +6,8 @@ en:
new: New μpost new: New μpost
new: new:
create: Create μpost create: Create μpost
edit:
edit: Edit μpost
delete: Delete μpost
confirm: Are you sure you want to delete this μpost?

View File

@ -12,5 +12,5 @@ Rails.application.routes.draw do
delete "log_out", to: "sessions#destroy_session" delete "log_out", to: "sessions#destroy_session"
get "confirm_email", to: "email_confirmations#confirm" get "confirm_email", to: "email_confirmations#confirm"
resources :microposts, only: [:index, :new, :create, :show] resources :microposts
end end