前提・実現したいこと
railsを用いて掲示板のような機能を実装中です。
「ログイン中のユーザーがトピックを作成できる。トピックに対してコメントをすることもできる。」というイメージで、
https://qiita.com/nojinoji/items/2034764897c6e91ef982 を参考に実装を進めているのですが、
フォームに入力したコメントの値の保存がうまくできずに困っています。
発生している問題・エラーメッセージ
エラーメッセージはブラウザに出ておらず、ログを見てもエラーは起きていません。
ただしブラウザでは、以下のCommentControllerのcreateアクションの中の、elseに入れてあったルートURLの方にリダイレクトされてしまっていることから、コメントが保存されていないらしいことがわかりました。つまり、おそらくですが入力(form_with周り)に関しては問題はないと思われます。それで、strong parameters周りが怪しいと踏んで色々調べてみてはいるのですが、解決策が見出せない状況です。
class CommentController < ApplicationController def create @comment = Comment.new(comment_params) @comment.user_id = current_user.id if @comment.save flash[:notice] = "コメントを投稿しました" redirect_to("/theater/index") else redirect_to("/") end end private def comment_params params.require(:comment).permit(:content) end end
該当のソースコード
/theater/show.html.erb
... <%= form_with model:[@theater, @comment],url: comment_create_path, local: true do |form| %> <%= form.text_area :content, class: "form-control" %> <br> <%= form.submit 'コメントする', class: "btn btn-primary" %> <% end %>
routes
... post "/comment/create" => "comment#create" ...
userモデル
... has_many :theaters has_many :comments ...
theaterモデル
... belongs_to :user has_many :comments, dependent: :destroy ...
commentモデル
belongs_to :user belongs_to :theater validates :content ,{presence: true}
補足情報(FW/ツールのバージョンなど)
ruby 2.5.0, rails 5.1.6, cloud9
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/12 19:50