前提・実現したいこと
ある一つのトピックの中に複数のユーザーがコメントできるようにしたいです。
発生している問題・エラーメッセージ
現在railsを使ってsnsサイトのようなものを開発しています。
コメント機能を実装しようとしてエラーが出てしまい、自分の力量じゃ解決できそうにないのでどなたか教えていただきたいです。
Started POST "/topics/12/post_comments" for 10.0.2.2 at 2019-08-17 06:26:49 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 (2.6ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC ↳ /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 Processing by PostCommentsController#create as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"0MTzYl2KkLGR7LWbPh/WMyrrrxNOu/znoFM9Tcut/asgFGCcQOFn/p7dbXZnw8XNIefiryUqrd31y9ptV1HumA==", "post_comment"=>{"post_comment_comment"=>"firstcomment"}, "commit"=>"コメントをする", "topic_id"=>"12"} Topic Load (2.6ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT ? [["id", 12], ["LIMIT", 1]] ↳ app/controllers/post_comments_controller.rb:3 Completed 500 Internal Server Error in 255ms (ActiveRecord: 13.4ms) ↓こちらがエラー文です NameError (undefined local variable or method `postcomment_params' for #<PostCommentsController:0x00005608c3d79cc8>): app/controllers/post_comments_controller.rb:4:in `create'
該当のソースコード
controller
1class PostCommentsController < ApplicationController 2 def create 3 @topic = Topic.find(params[:topic_id]) 4 @comment = @topic.post_comments.build(post_comment_params) 5 @comment.user_id = current_user.id 6 if @comment.save 7 render :index 8 end 9 end 10 11 def destroy 12 comment = PostComment.find(params[:id]) 13 topic = Topic.find(params[:topic_id]) 14 comment.destroy 15 redirect_to topic_path(topic) 16 end 17 18 private 19 def post_comment_params 20 params.require(post_comment).permit(:comment, :user_id, :topic_id) 21 end 22 23end
model
1class PostComment < ApplicationRecord 2 belongs_to :user 3 belongs_to :topic 4end 5
db
1 create_table "post_comments", force: :cascade do |t| 2 t.text "comment" 3 t.integer "user_id" 4 t.integer "topic_id" 5 t.datetime "created_at", null: false 6 t.datetime "updated_at", null: false 7 end 8
routes
1 resources :topics do 2 resource :user_topics, only:[:create, :destroy] 3 resource :post_comments, only:[:create, :destroy] 4 end 5 6
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/17 08:30
2019/08/17 08:33
2019/08/17 08:37