前提
DMのメッセージを誰のメッセージか分かるように右と左に振り分けて表示させたいです。
現在はDMの相手メッセージも自身のメッセージも同じく左に表示されているので自身のメッセージを右に表示させたいのですが、controllerから振り分けるのか、view箇所で振り分けることが可能なのでしょうか?
該当のソースコード
message.rb
rooms_controllers.rb/showアクション
1def show 2 @room = Room.find(params[:id]) 3 if Entry.where(user_id: current_user.id,room_id: @room.id).present? 4 @messages = @room.messages 5 @message = Message.new 6 @entries = @room.entries 7 else 8 redirect_back(fallback_location: root_path) 9 end 10 end
messages_controllers.rb/createアクション
views/rooms/show.html.erb
1<div class="chat-box <%= ' my-message' if m.user == current_user %>"> 2 <div class="chat-hukidashi"> 3 <%if m.user == current_user%> 4 <div class='dm-user my-message'> 5 <%= m.user.name %> 6 <%= image_tag 'room_logo.png', :alt => 'アプリロゴ', :class => 'topLogo, rounded-circle', size: '50x50' %> 7 </div> 8 <% else %> 9 <div class='dm-user'> 10 <%= image_tag 'room_logo.png', :alt => 'アプリロゴ', :class => 'topLogo, rounded-circle', size: '50x50' %> 11 <%= m.user.name %> 12 </div> 13 <% end %> 14 <div class="chat-content"> 15 <div class="chat-content-text"> 16 <p><%=safe_join(m.content.split("\n"),tag(:br))%></p> 17 </div> 18 <div class="chat-content-time"> 19 <%= m.created_at.strftime("%Y年%m月%d日 %H:%M")%> 20 </div> 21 </div> 22 </div> 23 </div>
!追記
room.scss
1.chat{ 2 height: auto; 3 padding: 20px; 4 border: solid 1px silver; 5 border-radius: 10px; 6 background-color: #FFFFEE; 7 .my-message{ 8 width: auto; 9 display: flex; 10 justify-content: flex-end; 11 order: 3; 12 } 13 }
!追記
自身のメッセージを右に配置することにしました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/07 21:36
退会済みユーザー
2022/11/08 06:05
2022/11/08 06:35
退会済みユーザー
2022/11/08 06:55
2022/11/08 06:59
退会済みユーザー
2022/11/08 07:33 編集
2022/11/08 07:40
退会済みユーザー
2022/11/08 08:49 編集
2022/11/08 09:00
2022/11/08 09:02
退会済みユーザー
2022/11/08 09:20 編集
退会済みユーザー
2022/11/08 11:23