Q&A
前提、実現したいこと
railsで動画投稿サイトを作成しています。
実際に動画を投稿する部分を作っているのですが上手くいきません。
動画投稿機能をつけるためにActiveStorage機能を実装しようとしました。
原因と解決策をご教授いただきたいです。
ソースコードなどで掲載に不足している部分があれば、指摘していただき次第、修正します。
質問箇所以外にもおかしな点があったら申し訳ございません。
発生している問題・エラーメッセージ
エラーメッセージ ActionView::Template::Error (undefined method `each' for nil:NilClass '.freeze; @movie.each do |movie| ^^^^^): 14: 15: <tbody> 16: #<div id="movies"> 17: <% @movie.each do |movie| %> 18: <tr> 19: #<%= render movie %> 20: <td><%= movie.title %></td> app/views/movies/index.html.erb:17
該当のソースコード
index.html.erb
1<p id="notice"><%= notice %></p 2 3<h1>Movie一覧</h1> 4 5<table> 6 <thead> 7 <tr> 8 <th>Title</th> 9 <th>Introduction</th> 10 <th colspan="3"></th> 11 </tr> 12 </thead> 13 14 <tbody> 15 #<div id="movies"> 16 <% @movie.each do |movie| %> 17 <tr> 18 #<%= render movie %> 19 <td><%= movie.title %></td> 20 <td><%= movie.introduction %></td> 21 <td><%= movie.movie.attached? ? 1 : 0 %></td> 22 <td><%= link_to '詳細', movie %></td> 23 <td><%= link_to '編集', edit_movie_path(moive) %></td> 24 <td><%= link_to '削除', movie, method: :delete, data: { confirm: 'Are you sure?' } %></td> 25 </tr> 26 <% end %> 27 </tbody> 28</table> 29 30<br> 31 32<%= link_to "追加", new_movie_path %> 33
movies_controller.rb
1class MoviesController < ApplicationController 2 before_action :set_movie, only: %i[ show edit update destroy ] 3 4 # GET /movies or /movies.json 5 def index 6 @movies = Movie.all 7 end 8 9 # GET /movies/1 or /movies/1.json 10 def show 11 end 12 13 # GET /movies/new 14 def new 15 @movie = Movie.new 16 end 17 18 # GET /movies/1/edit 19 def edit 20 end 21 22 # POST /movies or /movies.json 23 def create 24 @movie = Movie.new(movie_params) 25 26 respond_to do |format| 27 if @movie.save 28 format.html { redirect_to movie_url(@movie), notice: "Movie was successfully created." } 29 format.json { render :show, status: :created, location: @movie } 30 else 31 format.html { render :new, status: :unprocessable_entity } 32 format.json { render json: @movie.errors, status: :unprocessable_entity } 33 end 34 end 35 end 36 37 # PATCH/PUT /movies/1 or /movies/1.json 38 def update 39 respond_to do |format| 40 if @movie.update(movie_params) 41 format.html { redirect_to movie_url(@movie), notice: "Movie was successfully updated." } 42 format.json { render :show, status: :ok, location: @movie } 43 else 44 @movie = movie.all 45 format.html { render :edit, status: :unprocessable_entity } 46 format.json { render json: @movie.errors, status: :unprocessable_entity } 47 end 48 end 49 end 50 51 # DELETE /movies/1 or /movies/1.json 52 def destroy 53 @movie.destroy 54 55 respond_to do |format| 56 format.html { redirect_to movies_url, notice: "Movie was successfully destroyed." } 57 format.json { head :no_content } 58 end 59 end 60 61 private 62 # Use callbacks to share common setup or constraints between actions. 63 def set_movie 64 @movie = Movie.find(params[:id]) 65 end 66 67 # Only allow a list of trusted parameters through. 68 def movie_params 69 params.require(:movie).permit(:title, :introduction, :movie) 70 end 71end
routes.rb
1Rails.application.routes.draw do 2 resources :movies 3 devise_for :users 4 root to: "home#index" 5 6 resources :users 7 8end
試したこと
色々調べて、おそらくcreateアクションでsaveが失敗した場合?に、@movieにnil入ってしまっているため、エラーを起こしていることは分かったのですが、解決策が分からなかったです。
補足情報
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/03/08 08:05