現在、ツイッターにアプリケーションを作成しており、コメント機能の実装の際、以下のエラーが起きました。
NoMethodError in CommentsController#create undefined method `permit' for "ssssssssssssssssssss":String private def comments_params params.require(:comment).permit(:text).merge(user_id: current_user.id, tweet_id: params[:tweet_id]) end end
該当のソースコード
comments_controller.rb
class CommentsController < ApplicationController def create @comment = Comment.new(comments_params) @comment.save redirect_to tweet_path(@comment.tweet.id) end private def comments_params params.require(:comment).permit(:text).merge(user_id: current_user.id, tweet_id: params[:tweet_id]) end end
20200125131637_create_comments.rb
class CreateComments < ActiveRecord::Migration[5.2] def change create_table :comments do |t| t.text :text, null: false t.integer :user_id, null: false, foreign_key: true t.integer :tweet_id, null: false, foreign_key: true t.timestamps end end end
show.html.haml
= render "homes/header" .tweetshow-box .tweetshow-header = image_tag @tweet.user.image.url, class: "tweetshow-userimage" .tweetshow-username = @tweet.user.name = image_tag @tweet.picture.url, class: "tweetshow-contentimage" .tweetshow-contenttext = @tweet.text .comment-formbox = form_with model: @comment, url: tweet_comments_path(@tweet.id), method: :post, local: true do |f| = f.text_area :text, class: "commentbox", placeholder: "コメントする" = f.submit "送信", class: "comment-btn" .comment-listcontainer - @comments.each do|comment| .comment-listbox .comment-listheader = image_tag comment.user.image.url, class: "comment-userimage" .comment-username = comment.user.name .comment-box = comment.comment
comments_paramsの書き方が間違っているのでしょうか?
補足
viewでcommentの表示を行う前はコメントを作成することができていました。
お時間に余裕がございましたら教えていただけるとありがたいです。
Rails 5.2.4.1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/26 02:23