質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

1071閲覧

コメントしたユーザーの名前と写真を投稿する方法

umakichi

総合スコア44

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2022/02/14 01:11

現在、ruby on railsでアプリ作成をしています。
ドッグラン(dogrun)controllerのshowページから、新規コメントを投稿できるようにしているのですが、dogrun indexでコメント一覧を表示する際に、コメントをしたユーザーの名前と写真も表示させたいと思っております。コメントしたユーザーは、ログインしているユーザーで、user.id = comment.user_idで関連付けは出来ていて、ユーザーIDまでは表示出来ています。

ドッグランコントローラーのshowで、user-idは取得できましたが、このuser_idを元に、コメントをしたユーザーの名前と写真を上げたいが、どのように書いたらいいかわかりません。
dogrunコントローラーのshow、comment controllerのnew create、そして、dogrun/viewのindexに、それぞれどのように記入したらいいか教えていただきたいです。

comments_controller

1class CommentsController < ApplicationController 2 layout 'comment' 3 before_action :set_q 4 5 # GET /comments or /comments.json 6 def index 7 @comments = Comment.all 8 end 9 10 # GET /comments/1 or /comments/1.json 11 def show 12 @comment = Comment.find(params[:id]) 13 end 14 15 16 # GET /comments/1/edit 17 def edit 18 @comment = Comment.find(params[:id]) 19 end 20 21 # POST /comments or /comments.json 22 def create 23 @comment = Comment.new(comment_params) 24 @comment = current_user.comments.build(comment_params) 25 if @comment.save 26 flash[:notice] = "投稿完了しました" 27 redirect_to :dogruns 28 else 29 flash[:notice] = "投稿失敗しました" 30 render dogrun_path(params[:comment][:dogrun_id]) 31 end 32 end 33 34 35 36 # PATCH/PUT /comments/1 or /comments/1.json 37 def update 38 @comment = Comment.find(params[:id]) 39 respond_to do |format| 40 if @comment.update(comment_params) 41 format.html { redirect_to comment_url(@comment), notice: "Comment was successfully updated." } 42 format.json { render :show, status: :ok, location: @comment } 43 else 44 format.html { render :edit, status: :unprocessable_entity } 45 format.json { render json: @comment.errors, status: :unprocessable_entity } 46 end 47 end 48 end 49 50 # DELETE /comments/1 or /comments/1.json 51 def destroy 52 @comment = Comment.find(params[:id]) 53 @comment.destroy 54 55 respond_to do |format| 56 format.html { redirect_to comments_url, notice: "Comment was successfully destroyed." } 57 format.json { head :no_content } 58 end 59 end 60 61 private 62 # Only allow a list of trusted parameters through. 63 def comment_params 64 params.require(:comment).permit(:contents, :dogrun_id, :user_id, :title) 65 end 66 67 def set_q 68 @q = Dogrun.ransack(params[:q]) 69 end 70end 71

dogruns_controller

1class DogrunsController < ApplicationController 2 layout 'dogrun' 3 before_action :set_q 4 5 # トップページ 6 def index 7 @dogruns = Dogrun.all 8 @comments = Comment.all 9 end 10 11 #検索ページ 12 def search 13 @results = @q.result 14 end 15 16 # GET /dogruns/1 or /dogruns/1.json 17 def show 18 @dogrun = Dogrun.find(params[:id]) 19 @comment = Comment.new #新規コメント投稿 20 @comments = @dogrun.comments 21 end 22 23 # GET /dogruns/new 24 def new 25 @dogrun = Dogrun.new 26 end 27 28 # GET /dogruns/1/edit 29 def edit 30 @dogrun = Dogrun.find(params[:id]) 31 end 32 33 # POST /dogruns or /dogruns.json 34 def create 35 @dogrun = Dogrun.new(dogrun_params) 36 37 respond_to do |format| 38 if @dogrun.save 39 format.html { redirect_to dogrun_url(@dogrun), notice: "Dogrun was successfully created." } 40 format.json { render :show, status: :created, location: @dogrun } 41 else 42 format.html { render :new, status: :unprocessable_entity } 43 format.json { render json: @dogrun.errors, status: :unprocessable_entity } 44 end 45 end 46 end 47 48 # PATCH/PUT /dogruns/1 or /dogruns/1.json 49 def update 50 @dogrun = Dogrun.find(params[:id]) 51 respond_to do |format| 52 if @dogrun.update(dogrun_params) 53 format.html { redirect_to dogrun_url(@dogrun), notice: "Dogrun was successfully updated." } 54 format.json { render :show, status: :ok, location: @dogrun } 55 else 56 format.html { render :edit, status: :unprocessable_entity } 57 format.json { render json: @dogrun.errors, status: :unprocessable_entity } 58 end 59 end 60 end 61 62 # DELETE /dogruns/1 or /dogruns/1.json 63 def destroy 64 @dogrun = Dogrun.find(params[:id]) 65 @dogrun.destroy 66 67 respond_to do |format| 68 format.html { redirect_to dogruns_url, notice: "Dogrun was successfully destroyed." } 69 format.json { head :no_content } 70 end 71 end 72 73 private 74 75 76 # Only allow a list of trusted parameters through. 77 def dogrun_params 78 params.require(:dogrun).permit(:dogrun_name, :image, :address, :price, :pr) 79 end 80 81 def set_q 82 @q = Dogrun.ransack(params[:q]) 83 end 84end 85

dogruns/show.html.erb

1<p id="notice"><%= notice %></p> 2 3 <div class="room-show-image"> 4 <%= image_tag @dogrun.image ,class:'show-image'%> 5 </div> 6<p> 7 <strong>ドッグラン名</strong> 8 <%= @dogrun.dogrun_name %> 9</p> 10 11<p> 12 <strong>住所</strong> 13 <%= @dogrun.address %> 14</p> 15 16<p> 17 <strong>金額</strong> 18 <%= @dogrun.price %> 19</p> 20 21<p> 22 <strong>ドッグラン情報</strong> 23 <%= @dogrun.pr %> 24</p> 25 26<%= link_to 'Edit', edit_dogrun_path(@dogrun) %> 27 28<hr> 29 30 <h3>新規投稿</h3> 31 32 <%= form_with model: @comment do |f| %> 33 <table> 34 <tr> 35 <th><%= f.label :title, "タイトル" %></th> 36 <td><%= f.text_field :title, class:"posts-content" %></td> 37 </tr> 38 <tr> 39 <th><%= f.label :contents, "コンテンツ" %></th> 40 <td><%= f.text_field :contents, class:"posts-content" %></td> 41 </tr> 42 43 </table> 44 <%= f.hidden_field :dogrun_id, value: @dogrun.id %> 45 <div> 46 <ul> 47 <li><%= f.submit "投稿を完了する"%></li> 48 </ul> 49 </div> 50 <% end %> 51 52 <div> 53 <ul> 54 <li><%= link_to "ユーザー情報一覧に戻る", :dogruns %></li> 55 </ul> 56 </div> 57 58<h1>投稿済みコメント</h1> 59 60 <h3>投稿一覧</h3> 61 62 <table> 63 <thead> 64 <tr> 65 <th>USER</th> 66 <th>タイトル</th> 67 <th>コメント</th> 68 </tr> 69 </thead> 70 71 <tbody> 72 <% @comments.each do |comment| %> 73 <tr> 74 <td><%= comment.user_id %></td> 75 <td><%= comment.title %></td> 76 <td><%= comment.contents %></td> 77 78 79 </tr> 80 <% end %> 81 </tbody> 82 </table> 83 84 85 86<%= link_to 'Back', comments_path %> 87 88<hr> 89 90<%= link_to 'Back', dogruns_path %> 91

dogruns.index.html.erb

1<p id="notice"><%= notice %></p> 2 3<h1>Dogruns.indexページ</h1> 4 5 6<h1>検索ページ</h1> 7 8<div class="top_area"> 9 <div class="background-photo"> 10 11 12 <div class="search-box"><!--検索フォーム--> 13 14 <%= search_form_for @q, url: search_dogruns_path do |f| %> 15 16 <%= f.search_field :dogrun_name_or_address_cont ,placeholder: "キーワード検索" %> 17 <br> 18 <%= f.submit '検索' %> 19 <% end %> 20 21 </div> 22 </div> 23</div> 24 25<h1>Dogruns.indexページ</h1> 26 27<table> 28 <thead> 29 <tr> 30 <th>ドッグラン名</th> 31 <th>住所</th> 32 <th colspan="3"></th> 33 </tr> 34 </thead> 35 36 <tbody> 37 <% @dogruns.each do |dogrun| %> 38 <tr> 39 <td><%= dogrun.dogrun_name %></td> 40 <td><%= dogrun.address %></td> 41 <td><%= link_to 'Show', dogrun %></td> 42 <td><%= link_to 'Edit', edit_dogrun_path(dogrun) %></td> 43 <td><%= link_to 'Destroy', dogrun, method: :delete, data: { confirm: 'Are you sure?' } %></td> 44 </tr> 45 <% end %> 46 </tbody> 47</table> 48 49<br> 50<hr> 51 52<p id="notice"><%= notice %></p> 53 54<h1>投稿一覧</h1> 55 56<table> 57 <thead> 58 <tr> 59 <th>Title</th> 60 <th>Contents</th> 61 <th>Dogrun_id</th> 62 <th>user_id</th> 63 <th colspan="3"></th> 64 </tr> 65 </thead> 66 67 <tbody> 68 <% @comments.each do |comment| %> 69 <tr> 70 <td><%= comment.title %></td> 71 <td><%= comment.contents %></td> 72 <td><%= comment.dogrun_id %></td> 73 <td><%= comment.user_id %></td> 74 <td><%= link_to 'Show', comment %></td> 75 <td><%= link_to 'Edit', edit_comment_path(comment) %></td> 76 <td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td> 77 </tr> 78 <% end %> 79 </tbody> 80</table> 81 82<br> 83 84<%= link_to 'New Dogrun', new_dogrun_path %> 85 86<%= link_to 'New Comment', new_comment_path %> 87 88 89

comment.rb

1class Comment < ApplicationRecord 2 belongs_to :dogrun 3 belongs_to :user 4 5 6 7 validates :title, presence: true 8 validates :contents, presence: true 9end 10

user.rb

1class User < ApplicationRecord 2 has_one_attached :image 3 has_many :dogruns 4 has_many :comments 5 6 devise :database_authenticatable, :registerable, 7 :recoverable, :rememberable, :validatable 8end 9

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

class Comment に belongs_to :user が定義されてますから、
comment.user がそのcommentをした userです。
comment.user.name で名前がとれます。

投稿2022/02/14 01:35

winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

umakichi

2022/02/14 06:27

ありがとうございました。同様にimageの方も、comment.user.imageでとってくることが出来ました。ありがとうございます。
abel1303

2023/04/19 04:32 編集

Your writing demonstrated a real passion and enthusiasm for the subject matter.
guest

0

ドッグランコントローラーの表示ページからコメントを投稿するには、ドッグランのプロパティで新規コメント投稿権限を設定する必要があります。 tetris unblocked krunker

投稿2023/02/09 03:06

danielusa0106

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問