前提・実現したいこと
railsにて映画レビューサイトを作成しています。
映画詳細画面にコメント機能を実装しようとした際にエラーが発生してしまいました。
解決方法やソースコードをご教授いただきたいです。
発生している問題・エラーメッセージ
NameError in Public::Movies#show
Showing /home/ec2-user/environment/MovieBoard/app/views/public/movies/show.html.erb where line #15 raised
uninitialized constant Movie::MovieComment
Rails.root: /home/ec2-user/environment/MovieBoard
該当のソースコード
show.html.erb
<h1>顧客詳細</h1> <h5> 上映日時<%= @movie.day %> 製作国<%= @movie.country %> 上映時間<%= @movie.time %> </h5> <h2>タイトル</h2> <p><%= @movie.title %></p> <h2>あらすじ</h2> <p><%= @movie.body %></p> <div> <p>コメント件数:<%= @movie.movie_comments.count %></p> <% @movie.movie_comments.each do |movie| %> <%= movie_comment.customer.family_name %> <%= movie_comment.created_at.strftime('%Y/%m/%d') %><%= movie_comment.comment %> <% end %> </div> <div> <%= form_with model: [@movie, @movie_comment] do |f| %> <%= f.text_area :comment, rows: '5', placeholder: "コメントをここに" %> <%= f.submit "送信する" %> <% end %> </div> <%= link_to "一覧に戻る", admin_movies_path %>
movies_controller.rb
class Public::MoviesController < ApplicationController def index @movies = Movie.all end def show @movie = Movie.find(params[:id]) end end
movie_comments_controller.rb
class Public::MovieCommentsController < ApplicationController def create @movie = Movie.find(params[:movie_id]) @comment = current_customer.movie_comments.new(movie_comment_params) @comment.movie_id = movie.id @comment.save redirect_to movies_path end private def post_comment_params params.require(:post_comment).permit(:comment) end end
routes.rb
Rails.application.routes.draw do # 顧客用 scope module: :public do root to: 'homes#top' resources :movies, only: [:index, :show] do resources :movie_comments, only: [:create] end resources :publics, only: [:show, :edit, :update] end # URL /customers/sign_in ... devise_for :customers,skip: [:passwords], controllers: { registrations: "public/registrations", sessions: 'public/sessions' } # 管理者用 namespace :admin do resources :movies, only: [:new, :create, :index, :show, :edit, :update, :destroy] end # URL /admin/sign_in ... devise_for :admin, skip: [:registrations, :passwords] ,controllers: { sessions: "admin/sessions" } # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end
schema.rb
create_table "movie_comments", force: :cascade do |t| t.text "comment" t.integer "public_id" t.integer "movie_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end
試したこと
・movies_commentsのテーブル名がmovues_commentsになっていた為rails g migration change_movue_comments_to_movie_commentsを利用して名前変更を行いました。
・ファイル名の記述ミスの確認
→修正後特に相違点は見当たらなかったです。
回答1件
あなたの回答
tips
プレビュー