ユーザごとに個別のメモを表示する
This commit is contained in:
parent
67ccb4a281
commit
e7bee77eea
6 changed files with 28 additions and 6 deletions
|
@ -1,8 +1,9 @@
|
|||
class MemosController < ApplicationController
|
||||
before_action :set_memo, only: %i[edit update destroy]
|
||||
before_action :authenticate_user!
|
||||
|
||||
def index
|
||||
@q = Memo.ransack(params[:q])
|
||||
@q = current_user.memos.ransack(params[:q])
|
||||
@memos = @q.result(distinct: true)
|
||||
.order(created_at: :desc)
|
||||
.page(params[:page])
|
||||
|
@ -15,13 +16,15 @@ class MemosController < ApplicationController
|
|||
end
|
||||
|
||||
def new
|
||||
@memo = Memo.new
|
||||
@memo = current_user.memos.build
|
||||
end
|
||||
|
||||
def edit; end
|
||||
def edit
|
||||
@memo = current_user.memos.find(params[:id])
|
||||
end
|
||||
|
||||
def create
|
||||
@memo = Memo.new(memo_params)
|
||||
@memo = current_user.memos.build(memo_params)
|
||||
respond_to do |format|
|
||||
if @memo.save
|
||||
format.html { redirect_to memos_path, status: :see_other }
|
||||
|
@ -56,7 +59,10 @@ class MemosController < ApplicationController
|
|||
private
|
||||
|
||||
def set_memo
|
||||
@memo = Memo.find(params[:id])
|
||||
@memo = current_user.memos.find_by(id: params[:id])
|
||||
return unless @memo.nil?
|
||||
|
||||
redirect_to memos_path, alert: 'メモが見つかりませんでした'
|
||||
end
|
||||
|
||||
def memo_params
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
class Memo < ApplicationRecord
|
||||
belongs_to :user
|
||||
has_one_attached :image
|
||||
validates :content, presence: true
|
||||
|
||||
|
|
|
@ -3,4 +3,5 @@ class User < ApplicationRecord
|
|||
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :validatable
|
||||
has_many :memos
|
||||
end
|
||||
|
|
|
@ -10,6 +10,12 @@
|
|||
<%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module", as: "script" %>
|
||||
</head>
|
||||
|
||||
<% if current_user %>
|
||||
<div class="fixed top-4 left-4 text-sm text-gray-600 bg-gray-100 px-3 py-1 rounded-full">
|
||||
<%= current_user.email %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<body class="bg-gray-50">
|
||||
<% if user_signed_in? %>
|
||||
<div class="fixed top-4 right-4">
|
||||
|
|
5
db/migrate/20250224135719_add_user_to_memos.rb
Normal file
5
db/migrate/20250224135719_add_user_to_memos.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class AddUserToMemos < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_reference :memos, :user, null: false, foreign_key: true
|
||||
end
|
||||
end
|
5
db/schema.rb
generated
5
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_02_24_005433) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_02_24_135719) do
|
||||
create_table "active_storage_attachments", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
|
@ -43,6 +43,8 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_24_005433) do
|
|||
t.text "content", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "user_id", null: false
|
||||
t.index ["user_id"], name: "index_memos_on_user_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
|
@ -59,4 +61,5 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_24_005433) do
|
|||
|
||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "memos", "users"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue