2016-05-26 6 views
0

私はレールで映画レビューアプリを作ろうとしています。これには、ムービーイメージとテキストフィールドの追加が含まれます。私は画像をアップロードするためにペーパークリップの宝石を使用しています。映画の追加中にこのエラーが発生します。作品中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 
+0

あなたの '@categories = Category.all.map {| c | [c.name、c.id]} '行 –

+0

' Category'テーブルに 'record'があります –

+0

私のカテゴリテーブルにはレコードがあります。私は質問を編集し、私の質問にカテゴリモデルスニペットを追加します –

答えて

0

あなたはMovies#createアクションで@categoriesインスタンス変数を定義していません。 create方法に

@categories = Category.all.map{ |c| [c.name, c.id] } 

は、このコードを追加します。

+0

コードを作成メソッドに追加しました。エラーを表示していません。しかし、まだ新しい映画を追加することはできません。私は、作成ボタンをクリックすると、作成ページにリダイレクトされます。 –

関連する問題