rails初心者です。
初歩的な質問失礼します。
現在、画像を投稿し、その画像の詳細ページでコメントを投稿できる機能を
実装しようとしているのですが、Routing Errorが出ており、解決ができず
止まってしまっています。
pictures_controller
comments_controller
があり、
pictures_controllerのshow.html.haml内でcomments_controllerのcreateアクションで
コメントを投稿し、redirect_toでそのままpictures_controllerのshow.html.hamlに
戻るようにしたいと考えています。
下記がそれぞれのコードです。
rails
1//pictures_controller 2 def show 3 @picture = Picture.find(params[:id]) 4 @comments = @picture.comments.includes(:user) 5 end 6 7//comments_controller 8 def create 9 @comment = Comment.create(text: comment_params[:text], picture_id: comment_params[:picture_id], user_id: current_user.id) 10 redirect_to picture_path(@comment.picture.id) 11 end 12 13//pictures_controllerのshow.html.haml 14 .details-content__comment 15 - if current_user 16 .details-content__comment__form 17 = form_with model: @comment, local: true do |f| 18 .details-content__comment__form__group 19 = f.text_field :comment, class: 'details-content__comment__form__group__text', placeholder: 'コメント' 20 = f.submit 'コメントする', class: 'btn btn-primary'
pictureとcommentはそれぞれアソシエーションは組んでいます。
上記のコードでコメントを投稿すると、下記のエラー文が出ます。
Routing Error
No route matches [POST] "/pictures/6"
pictures_controllerを下記のように修正しましたが、
変わりませんでした。
rails
1//pictures_controller 2 def show 3 @picture = Picture.find(params[:id]) 4 @comment = @comment 5 @comments = @picture.comments.includes(:user) 6 end 7 8
routes.rbは下記の内容です。
rails
1//routes.rb 2Rails.application.routes.draw do 3 devise_for :users 4 root 'pictures#index' 5 resources :pictures do 6 resources :comments, only: [:create] 7 end 8 resources :users, only: [:show] 9end 10
どなたかご教示頂けますと幸いです。
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー