jeudi 26 mai 2016

Rails- paperclip- NoMethodError

I'm trying to make a movie review app in rails. This includes adding movie image and text fields. I'm using the paperclip gem for image upload. I am getting this error while adding movie.

NoMethodError in Movies#create

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

I am rendering a form partial in my movies/new,html.erb. Following is code snippet from movies/_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 %>

Movies Controller

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: I have added image parameter in the movie_params method which is their in the private section- Following is the code snippet

def movie_params
        params.require(:movie).permit(:title, :description, :director, :category_id, :movie_img)
    end

Movies Model

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

Aucun commentaire:

Enregistrer un commentaire