私はレールで映画レビューアプリを作ろうとしています。これには、ムービーイメージとテキストフィールドの追加が含まれます。私は画像をアップロードするためにペーパークリップの宝石を使用しています。映画の追加中にこのエラーが発生します。作品中Rails- paperclip- NoMethodError
NoMethodError#
Showing - MovieReview/app/views/movies/_form.html.erb where line #2 raised:
undefined method `map' for nil:NilClass
Trace of template inclusion: app/views/movies/new.html.erb
Rails.root: /Review/MovieReview
を作成し、私は私の映画/新しい、html.erbで部分的にフォームをレンダリングしています。以下の 映画/ _form.html.erb
<%= simple_form_for @movie, :html => { :multipart => true} do |f| %>
<%= select_tag(:category_id, options_for_select(@categories), :prompt => "Select a Category") %>
<%= f.file_field :movie_img %>
<%= f.input :title, label: "Movie Title" %>
<%= f.input :description %>
<%= f.input :director %>
<%= f.button :submit %>
作品コントローラ
class MoviesController < ApplicationController
def new
@movie = current_user.movies.build
@categories = Category.all.map{ |c| [c.name, c.id] }
end
def create
@movie = current_user.movies.build(movie_params)
@movie.category_id = params[:category_id]
if @movie.save
redirect_to root_path
else
render 'new'
end
end
PSからのコードスニペットです:私は彼らの中にあるmovie_params方法で画像パラメータを追加しましたプライベートセクション - 次はコードスニペットです
def movie_params
params.require(:movie).permit(:title, :description, :director, :category_id, :movie_img)
end
作品モデル
class Movie < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_attached_file :movie_img, styles: { movie_index: "250x350>", movie_show: "325x475>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :movie_img, content_type: /\Aimage\/.*\Z/
end
カテゴリーモデル
class Category < ActiveRecord::Base
has_many :movies
end
あなたの '@categories = Category.all.map {| c | [c.name、c.id]} '行 –
' Category'テーブルに 'record'があります –
私のカテゴリテーブルにはレコードがあります。私は質問を編集し、私の質問にカテゴリモデルスニペットを追加します –