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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

254閲覧

undefined method `coments' for nil:NilClass のエラー解決

takeshi075

総合スコア3

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/06/23 11:23

Ruby on Rails でトレーニング投稿サイトを作っています。
コメント投稿機能を実装中に以下のエラーメッセージが発生しました。
実現したいことは、投稿一覧画面で任意の投稿に対してコメントを作成し、表示させる様にしたいです。

イメージ説明

##該当のソースコード

ruby

1class PostsController < ApplicationController 2 before_action :authenticate_user! 3 4 def index 5 @posts = Post.all 6 @coments = @post.coments 7 @coment = Coment.new 8 end 9 10 def show 11 @post = Post.find(params[:id]) 12 @coments = @post.coments.build 13 @coment = Coment.build 14 end 15 16 def new 17 @post = Post.new 18 end 19 20 def create 21 @post = Post.new(post_params) 22 if @post.save 23 redirect_to root_path 24 else 25 render "new" 26 end 27 end 28 29 def destroy 30 post = Post.find(params[:id]) 31 if post.user_id == current_user.id 32 post.destroy 33 end 34 redirect_to user_path(current_user) 35 end 36 37 38 private 39 def post_params 40 params.require(:post).permit(:memo, :image,:menu1,:menu2,:menu3,:menu4,:menu5 ).merge(user_id: current_user.id) 41 end 42 43end 44

##関連ファイルのソースコード

ルーティング

ruby

1Rails.application.routes.draw do 2 get 'users/show'=> "users#show" 3 devise_for :users, controllers: { registrations: 'users/registrations' } 4 5 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 6 root to: "posts#index" 7 resources :posts, only: [:index, :new, :create,:destroy] 8 resources :users, only: [:show, :new, :edit] 9 resources :coments, only: [:create] 10end 11 12

コメントコントローラー

ruby

1class ComentsController < ApplicationController 2 before_action :authenticate_user! 3 4 def create 5 post = Post.find(params[:post_id]) 6 @coment = post.coments.build(coment_params) 7 @coment.user_id = current_user.id 8 if @coment.save 9 redirect_back(fallback_location: root_path) 10 else 11 redirect_back(fallback_location: root_path) 12 end 13 14 end 15 16 private 17 def coment_params 18 params.require(:coment).permit(:text,:post_id,:user_id) 19 end 20end 21 22end 23

投稿一覧画面

ruby

1.wrapper 2 - @posts.each do |post| 3 .main 4 .main__content 5 = link_to user_path(post.user), class:"main__content__profile" do 6 .main__content__profile__image 7 - if post.user.image? 8 = image_tag post.user.image.url,class:"icon" 9 - else 10 = image_tag "/icon/ifn0013.png",class:"icon" 11 .main__content__profile__list 12 .main__content__profile__list__name 13 = post.user.name 14 .main__content__profile__list__day 15 = post.created_at.strftime("%Y年%m月%d日 %H時%M分") 16 .main__content__list 17 .main__content__list__text 18 .main__content__list__text__title 19 一言メモ 20 .main__content__list__text__detail 21 = post.memo 22 .main__content__list__menuImage 23 .main__content__list__menuImage__menu 24 .main__content__list__menuImage__menu__title 25 トレーニングメニュー 26 .main__content__list__menuImage__menu__detail 27 .main__content__list__menuImage__menu__detail__1 28 = post.menu1 29 .main__content__list__menuImage__menu__detail__1 30 = post.menu2 31 .main__content__list__menuImage__menu__detail__1 32 = post.menu3 33 .main__content__list__menuImage__menu__detail__1 34 = post.menu4 35 .main__content__list__menuImage__menu__detail__1 36 = post.menu5 37 .main__content__list__menuImage__image 38 .main__content__list__menuImage__image__title 39 画像 40 .main__content__list__menuImage__image__detail 41 = image_tag post.image.url 42 .main__content__list__coments 43 .hidden_box 44 %label{:for => "label1"} コメントを見る ( 1.クリック 2.スクロールして確認) 45 %input#label1{:type => "checkbox"} 46 .hidden_show 47 .hidden_show__coments 48 .hidden_show__coments__profile 49 = link_to root_path do 50 .hidden_show__coments__profile__image 51 = image_tag "/icon/ニューヨーク.jpg",class:"icon" 52 .hidden_show__coments__profile__name 53 たけ 54 .hidden_show__coments__profile__day 55 20206156 .hidden_show__coments__box 57 アイウエオ 58 .hidden_show 59 .hidden_show__coments 60 .hidden_show__coments__profile 61 = link_to root_path do 62 .hidden_show__coments__profile__image 63 = image_tag "/icon/ニューヨーク.jpg",class:"icon" 64 .hidden_show__coments__profile__name 65 たけ 66 .hidden_show__coments__profile__day 67 20206168 .hidden_show__coments__box 69 アイウエオ 70 .main__content__list__coments 71 = form_with(model: [@post, @coment], local: true) do |f| 72 = f.text_field :text 73 = f.hidden_field :post_id, value: @post.id 74 = f.submit 'コメントする', class: "btn btn-primary"

postマイグレーションファイル

ruby

1class CreatePosts < ActiveRecord::Migration[5.2] 2 def change 3 create_table :posts do |t| 4 t.text :memo , null: false 5 t.text :menu1 , null: false 6 t.text :menu2 7 t.text :menu3 8 t.text :menu4 9 t.text :menu5 10 t.string :image 11 t.references :user , null: false,foreign_key: true 12 t.timestamps 13 end 14 end 15end

postモデル

ruby

1class Post < ApplicationRecord 2 belongs_to :user 3 has_many :coments 4 mount_uploader :image, ImageUploader 5 6 validates :memo, presence: true 7 validates :menu1, presence: true 8 9end

coment マイグレーションファイル

ruby

1class CreateComents < ActiveRecord::Migration[5.2] 2 def change 3 create_table :coments do |t| 4 t.text :text ,null: false 5 t.references :post ,null: false, foreign_key: true 6 t.references :user ,null: false, foreign_key: true 7 t.timestamps 8 end 9 end 10end

comentモデル

ruby

1class Coment < ApplicationRecord 2 belongs_to :post,optional: true 3 belongs_to :user 4end

##試したこと
まだコメントが投稿されていないことによるエラーだと判断し、postコントローラの

ruby

1@coments = @post.coments 2@coment = Coment.new

をコメントアウトしてみましたが以下のエラーがでます。

Nil location provided. Can't build URI.

コメントを作るために上記のコードを記述していたので、消してはいけないと思いますが、消さないと進まない様な、
堂々巡りです。
エラーについて検索しましたが、思う様なものが見つからず、2日間程経過しています。
どなたかご教授いただけないでしょうか?
よろしくお願い致します。
##補足情報(FW/ツールのバージョンなど)
MacBook Pro 13
macOs 10.15.4
rails 5.2.3
PostgreSQL. Versions 9.1

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

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

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

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

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

guest

回答1

0

ベストアンサー

@postがnilなんでは。値を確認してみよう

投稿2020/06/23 12:01

y_waiwai

総合スコア87749

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

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

takeshi075

2020/06/24 11:52

返信ありがとうございます! binding.pry で確認したところ @postはnilでした。 @postsには投稿したデータが入っていたのでpostコントローラーを@postsに変更しました。 すると、エラー内容が NoMethodError in PostsController#index undefined method `coments' for #<Post::ActiveRecord_Relation:0x00007fe99fef39f0> に変わりました。 これは解決に近づいているのでしょうか? それとも見当違いな修正をしているのでしょうか? ご教授よろしくお願い致します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問