前提
カレンダーを使った予定管理アプリを開発中です。
実現したいこと
devise userを使用して本人しか予定を見れないようにしたいのですが、他のユーザーの予定もカレンダーに記載されてしまっています。どうすれば本人だけの予定を表示できますか?
該当のソースコード
app/controllers/blogs_controller.rb
1class BlogsController < ApplicationController 2 before_action :authenticate_user! 3 4 def index 5 @blogs = Blog.all 6 @blog = Blog.new 7 end 8 9 def new 10 @blog = Blog.new 11 end 12 13 def show 14 @blog = Blog.find(params[:id]) 15 end 16 17 def create 18 Blog.create(blog_parameter) 19 redirect_to blogs_path 20 end 21 22 def destroy 23 @blog = Blog.find(params[:id]) 24 @blog.destroy 25 redirect_to blogs_path, notice:"削除しました" 26 end 27 28 def edit 29 @blog = Blog.find(params[:id]) 30 end 31 32 def update 33 @blog = Blog.find(params[:id]) 34 if @blog.update(blog_parameter) 35 redirect_to blogs_path, notice: "編集しました" 36 else 37 render 'edit' 38 end 39 end 40 41 private 42 43 def blog_parameter 44 params.require(:blog).permit(:title, :content, :start_time).merge(user_id: current_user.id) 45 end 46end 47
app/models/blog.rb
1class Blog < ApplicationRecord 2 belongs_to :user 3 4 validates :title, presence: true 5 validates :start_time, presence: true 6end
app/models/user.rb
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_many :blogs 8end
app/views/blogs/index.html.erb
1<header> 2 <div class="header-left"> 3 <%= link_to image_tag("logo.png", class:"header-logo"), "/" %> 4 </div> 5 6 <div class="header-right"> 7 <ul class="header-right-ul"> 8 <li><%= link_to 'ToDoリスト', root_path, class: "header-link" %></li> 9 <li><%= link_to 'ログアウト', destroy_user_session_path, method: :delete, class: "header-link" %> </li> 10 </ul> 11 </div> 12 13</header> 14 15 16 17<div class="toppage"> 18<h1 class="top"></h1> 19<br> 20 21<div class="row"> 22<div class="col-md-4"> 23 <div class="schedule">今後の予定一覧</div> 24<table class="table"> 25 <thead class="thead-light"> 26 <tr class="border"> 27 <th class="border"><p class="border-text">タイトル</p></th> 28 <th class="border"><p class="border-text">日程</p></th> 29 </tr> 30 </thead> 31 32 <tbody> 33 <% @blogs.each do |blog| %> 34 <tr> 35 <td><%= link_to blog.title , blog %></td> 36 <td><%= blog.start_time.strftime("%m-%d") %></td> 37 </tr> 38 <% end %> 39 </tbody> 40</table> 41 42 43 44<%= form_with(model: @blog, local: true) do |form| %> 45 46 <div class="title"> 47 <div class="form-tag">タイトル</div> 48 <%= form.text_field :title, class:"form-tag-box"%> 49 </div> 50 51 52 <div class="content"> 53 <div class="form-tag">内容</div> 54 <%= form.text_field :content, class:"naiyou-box"%> 55 </div> 56 57 <div class="time"> 58 <div class="form-tag">時間</div> 59 <%= form.datetime_select :start_time %> 60 </div> 61 62 <div class="submit"> 63 <%= form.submit class:"submit-box"%> 64 </div> 65<% end %> 66</div> 67 68<br> 69 70 71<br> 72<div class="col-md-8"> 73 <p class="user-calendar"><%= current_user.name %>さんのカレンダー</p> 74<%= month_calendar events: @blogs do |date, blogs| %> 75 <%= date.day %> 76 <% blogs.each do |blog| %> 77 <div> 78 <%= link_to blog.title, blog %> 79 </div> 80 <% end %> 81<% end %> 82</div> 83</div> 84</div> 85 86<footer> 87 88</footer>
試したこと
<% if current_user.id == @blog.user_id %>を記述してみると、今度は何も表示されなくなりました。
app/views/blogs/index.html.erb
1header> 2 <div class="header-left"> 3 <%= link_to image_tag("logo.png", class:"header-logo"), "/" %> 4 </div> 5 6 <div class="header-right"> 7 <ul class="header-right-ul"> 8 <li><%= link_to 'ToDoリスト', root_path, class: "header-link" %></li> 9 <li><%= link_to 'ログアウト', destroy_user_session_path, method: :delete, class: "header-link" %> </li> 10 </ul> 11 </div> 12 13</header> 14 15 16 17<div class="toppage"> 18<h1 class="top"></h1> 19<br> 20 21<div class="row"> 22<div class="col-md-4"> 23 <div class="schedule">今後の予定一覧</div> 24<table class="table"> 25 <thead class="thead-light"> 26 <tr class="border"> 27 <th class="border"><p class="border-text">タイトル</p></th> 28 <th class="border"><p class="border-text">日程</p></th> 29 </tr> 30 </thead> 31 32 <% if current_user.id == @blog.user_id %> 33 <tbody> 34 <% @blogs.each do |blog| %> 35 <tr> 36 <td><%= link_to blog.title , blog %></td> 37 <td><%= blog.start_time.strftime("%m-%d") %></td> 38 </tr> 39 <% end %> 40 </tbody> 41 <% end %> 42</table> 43 44 45 46<%= form_with(model: @blog, local: true) do |form| %> 47 48 <div class="title"> 49 <div class="form-tag">タイトル</div> 50 <%= form.text_field :title, class:"form-tag-box"%> 51 </div> 52 53 54 <div class="content"> 55 <div class="form-tag">内容</div> 56 <%= form.text_field :content, class:"naiyou-box"%> 57 </div> 58 59 <div class="time"> 60 <div class="form-tag">時間</div> 61 <%= form.datetime_select :start_time %> 62 </div> 63 64 <div class="submit"> 65 <%= form.submit class:"submit-box"%> 66 </div> 67<% end %> 68</div> 69 70<br> 71 72 73<br> 74<div class="col-md-8"> 75 <p class="user-calendar"><%= current_user.name %>さんのカレンダー</p> 76<%= month_calendar events: @blogs do |date, blogs| %> 77 <%= date.day %> 78 <% if current_user.id == @blog.user_id %> 79 <% blogs.each do |blog| %> 80 <div> 81 <%= link_to blog.title, blog %> 82 </div> 83 <% end %> 84 <% end %> 85<% end %> 86</div> 87</div> 88</div> 89 90<footer> 91 92</footer>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/22 02:53