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

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

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

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

Q&A

解決済

1回答

567閲覧

ネストしているコントローラでの保存記述

mikan0w0

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2020/10/19 07:09

編集2020/10/19 07:44

前提・実現したいこと

カテゴリーを選択して、コンテンツを作り、そのコンテンツにコメントを残そうとしています。

ルーテイングは以下のようになっています。

ruby

1 resources :categories, only: [:index, :new, :create] do 2 resources :contents do 3 resources :comments 4 end 5 end

コンテンツまでは下記の記述で保存できました

ruby

1 def create 2 @category = Category.find(params[:category_id]) 3 @content = @category.contents.new(content_params) 4 @content.save 5 end 6 7 private 8 9 def content_params 10 params.require(:content).permit(:content, :category_id).merge(user_id: current_user.id) 11 end 12

試したこと

コメントの入力フォーム

html

1<div class="create-area"> 2 <%= form_with model: [@category, @content, @comment], class: "form", local: true do |f|%> 3 <form class="form"> 4 <div class="form2"> 5 <%= f.text_field :comment, placeholder: 'type a comment', class: 'input-comment' %> 6 </div> 7 <%= f.submit 'SEND', class: 'send-btn' %> 8 </form> 9 <% end %> 10</div>

コメントのコントローラー

ruby

1 def create 2 @category = Category.find(params[:category_id]) 3 @content = Content.find(params[:content_id]) 4 @comment = @content.comments.new(comment_params) 5 @comment.save 6 end 7 8 private 9 10 def comment_params 11 params.require(:comment).permit(:comment, :content_id).merge(user_id: current_user.id) 12 end

コメントのcreateメソッドにきたパラメーター

Parameters: {"authenticity_token"=>"d1cssjlsFeCpwmnmzQeDkI3x49j/liIviVIVjeSr2LU8CtCXPz/mfZeFs5skbW8w+gDlYseQ/sV7Il8Ta7oTyg==", "comment"=>{"comment"=>"go-lf2"}, "commit"=>"SEND", "category_id"=>"1", "content_id"=>"1"}

###エラーメッセージ

NoMethodError in CommentsController#create undefined method `comments' for #<Content:

上記のメソッド内@comment = @content.comments.new(comment_params)に問題があるようです。
記述を色々試しましたがうまくいきません。

初めてアプリケーションを一から作成しています。
お見苦しいところは多々あると思いますが、どうかご指導ください。

追記ーーーー

アソシエーション

user

1 devise :database_authenticatable, :registerable, 2 :recoverable, :rememberable, :validatable 3 has_many :contents, through: :comments 4 has_many :categories, through: :contents 5 has_many :comments

category

1 belongs_to :user 2 has_many :contents 3 has_many :users, through: :contents

content

1 belongs_to :user 2 belongs_to :category 3 has_many :user, through: :comments

comment

1 belongs_to :user 2 belongs_to :content

となっています

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

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

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

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

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

guest

回答1

0

自己解決

###アソシエーションの変更によって解決しました

user

1 devise :database_authenticatable, :registerable, 2 :recoverable, :rememberable, :validatable 3 has_many :contents 4 has_many :categories 5 has_many :categories, through: :contents 6 has_many :comments

content

1 belongs_to :user 2 belongs_to :category 3 has_many :comments 4 has_many :user, through: :comments

投稿2020/10/19 08:33

mikan0w0

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問