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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

3回答

970閲覧

rails コメント機能の実装について

castle

総合スコア6

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

1クリップ

投稿2019/12/02 08:05

編集2019/12/08 04:38

rails初心者です。
現在、ログインしたuserが店(spot)のレビューコメントを投稿できる機能を作っていますが、投稿した内容を表示できずに困っています。
以下を参考に

https://sadah.github.io/rails-training/ja/004_comments.html

~~userモデル、spotモデルと、投稿するコメント用のcommentモデルを用意しそれぞれ紐づけています。
~~

12/7追記 上記は解決しましたが現在コメントを投稿した User名が表示できず困っています。
解決方法の欄に詳細を記入しております

user

1・・・ 2has_many :spots 3has_many :comments 4・・・

spot

1class Spot < ApplicationRecord 2 validates :name, presence: true, length: { maximum: 50 } 3 validates :address, presence: true, length: { maximum: 100 } 4 validates :description, presence: true, length: { maximum: 255 } 5 6 belongs_to :user, optional: true 7 has_many :comments 8end

comment

1class Comment < ApplicationRecord 2 belongs_to :user, optional: true 3 belongs_to :spot, optional: true 4 validates :content, presence: true, length: { maximum: 500 } 5end

comment作成のcreateアクションは、

class CommentsController < ApplicationController before_action :require_user_logged_in def create @comment = Comment.new(comment_params) if @comment.save flash[:success] = 'コメントを投稿しました。' redirect_to root_url else flash.now[:danger] = "コメントに失敗しました。" render 'toppages/index' end end def destroy end private def comment_params params.require(:comment).permit(:content) end end

spots_controllerのshowアクション

def show @spot = Spot.find(params[:id]) @comments = @spot.comments.includes(:user).order(id: :desc).page(params[:page]) @comment = @spot.comments.build(user_id: current_user.id) if current_user end

コメントの表示用にview/comments/_reviw.html.erbを作成

<ul class="list-unstyled"> <% comments.each do |comment| %> <li class="media mb-3"> <img class="mr-2 rounded" src="<%= gravatar_url(comment.user, { size: 50 }) %>" alt=""> <div class="media-body"> <div> <%= link_to comment.user.name, user_path(comment.user) %> <span class="text-muted">posted at <%= comment.created_at %></span> </div> <div> <p><%= comment.content %></p> </div> </div> </li> <% end %> <%= paginate comments %> </ul>

view/spot/show.html.erb

<h1 class="text-center mb-5"><%= @spot.name %></h1> <article class="mt-5"> <table class="mb-5"> <tbody> <tr> <td>住所</td> <td><%= @spot.address %></td> </tr> </tbody> </table> <h3><%= @spot.name%>の情報</h3> <p><%= @spot.description %></p> </article> <!--レビューコメント--> <% if logged_in? %> <div class="row"> <aside class="col-sm-4"> <%= form_with(model: @comment, local: true) do |f| %> <div class="form-group"> <%= f.text_area :content, class: 'form-control', rows: 5 %> </div> <%= f.submit 'Post', class: 'btn btn-primary btn-block' %> <% end %> </aside> <div class="col-sm-8"> <%= render 'comments/review', comments: @comments %> </div> </div> <% else %> <div class="text-center"> <h1>会員登録で口コミができます!</h1> <%= link_to 'Sign up now!', signup_path, class: 'btn btn-lg btn-primary' %> </div> <% end %>

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

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

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

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

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

guest

回答3

0

自己解決

viewを次のように変更することで解決しました。

spots/show.html.erb

<div class="container"> <h1 class="text-center mb-5"><%= @spot.name %></h1> <div class="text-center"> <% if @spot.image.attached? %> <%= image_tag @spot.image, :size =>'800x500' %> <% end %> </div> <article class="mt-5"> <table class="mb-2"> <h3><%= @spot.name%>の情報</h3> <tbody> <tr> <td>住所</td> <td><%= @spot.address %></td> </tr> </tbody> </table> <p><%= @spot.description %></p> </article> <% if logged_in? %> <%= render 'favorites/like_button', spot: @spot %> <!--口コミ--> <% @comments.each do |c| %> <div>  <% unless c.user.blank? %>    <%= link_to c.user.name, user_path(c.user_id)%>   <% end %>   <h5><%= c.title %></h5>     <%= c.content %>   </div>     <% end %>      <!--口コミ投稿フォーム--> <div class="jumbotron bg-light"> <h4>口コミを投稿する</h4> <%= form_with model: [@spot, @comment], local: true do |f| %> <div class="form-group"> <%= f.label :title, "タイトル"%> <%= f.text_field :title, class: "form-control" %> </div> <div class="form-group"> <%= f.label :content, "コメント" %> <%= f.text_area :content, class: "form-control" %> </div> <%= f.submit 'コメントする', class: "btn btn-primary" %> <% end %> </div> <% else %> <div class="text-center"> <h5 class="mt-5">口コミお気に入り機能を使うにはログインしてください</h5> <p>会員登録がまだの方は → <%= link_to '会員登録', signup_path %> </p> </div> <% end %> </div>

投稿2019/12/11 05:43

編集2019/12/11 05:44
castle

総合スコア6

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

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

0

情報が足りないので想像逞しくしてになります。

おそらく comment にuserが割り当てられていないのでしょう。

class Comment < ApplicationRecord belongs_to :user, optional: true

とoptionalになっているということは、そうしないとvalidationで引っかかるからつけた。
ということはuserが割り当てられていない。

Commentの登録のところに戻る必要がありそうです

投稿2019/12/11 04:01

winterboum

総合スコア23344

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

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

0

次のように修正すると投稿出来るようになりました。
ですがコメントを投稿したUserの名前を表示させたいのですが、viewのc.user.nameでNoMethodErrorが出てしまい解決できず、困っております。

こちらを参考にしました。
https://qiita.com/nojinoji/items/2034764897c6e91ef982

comments_controllerのcreateアクションを

def create @spot = Spot.find(params[:spot_id]) @comment = @spot.comments.build(comment_params) @comment.user_id = current_user.id if @comment.save flash[:success] = "コメントしました" redirect_back(fallback_location: root_path) else flash[:danger] = "コメントできません" redirect_back(fallback_location: root_path) end   ・・・・

spots_controllerのshowアクションを

def show @spot = Spot.find(params[:id]) @comment = @spot.comments.build @comments = @spot.comments end

spots/viewを

<!--口コミ--> <div class="mb-5 mt-5"> <h2>コメント一覧</h2> <% if @comments.any? %> <% @comments.each do |c| %> <p><%= link_to "#{c.user.name}さん", user_path(c.user.id) %></p> <%= c.title %> <%= c.content %> <% end %> <% end %> </div> <!--口コミ投稿フォーム--> <div class="jumbotron bg-light"> <h4>口コミを投稿する</h4> <%= form_with model: [@spot, @comment], local: true do |f| %> <div class="form-group"> <%= f.label :title, "タイトル"%> <%= f.text_field :title, class: "form-control" %> </div> <div class="form-group"> <%= f.label :content, "コメント" %> <%= f.text_area :content, class: "form-control" %> </div> <%= f.submit 'コメントする', class: "btn btn-primary" %> <% end %> </div>

投稿2019/12/08 04:29

編集2019/12/08 04:32
castle

総合スコア6

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

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

winterboum

2019/12/11 03:58

エラーメッセージを載せる様にして下さい。どのfileの何行目という情報もあるのであまり省略せずに。
winterboum

2019/12/11 05:11

画像貼り付けではなく、メッセージのtext貼り付けの方が、回答書くときコピペできるのでうれしいです。 想像通りですので、回答の方を見て下さい
castle

2019/12/11 05:43

ありがとうございます。 名前の表示ができ、解決することができました。 textの貼り付けに関してもご指摘ありがとうございます。m(_ _)m
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問