0

I'm trying to upload image using paperclip. But the exception "NoMethodError in Shop::ProductsController#create undefined method `chomp' for nil:NilClass" occurs. Can anyone see and highlight the problem please?

Here is Gemfile code

gem 'paperclip', '~> 4.2.1'

Controller

class Shop::ProductsController < ApplicationController

    layout "shop"


  def index

    @products = Product.find_by_shop_id(session[:shop_id]).sort_by_name
    # @products = Product.products.sort_by_name

  end

  def show
    @product = Product.find(params[:id])
  end

  def new
    @product = Product.new
  end

  def edit
    @product = Product.find(params[:id])
  end

  def create

    @product = Product.new(product_params)

    @product.shop = Shop.find(session[:shop_id].to_i)
    if @product.save
        redirect_to :action => 'new'
    else
        render 'new'
    end

  end

  def update
    @product = Product.find(params[:id])

    if @product.update(product_params)
        redirect_to admin_products_path
    else
        render 'edit'
    end

  end

  def destroy
        @product = Product.find(params[:id])
        @product.destroy

        redirect_to admin_products_path     
  end

  private
    def product_params
        params.require(:product).permit(:name, :desc, :price, :display, :status)
    end

end

View

<%= form_for [:shop, @product] do |f| %>

                  <div class="box-body">
                    <div class="form-group">
                      <%= f.label :name %>
                      <%= f.text_field :name, :class => 'form-control', :placeholder => 'City name' %>
                    </div>
                    <div class="form-group">
                      <%= f.label "Description" %>
                      <%= f.text_area :desc, :class => 'form-control', :placeholder => 'Write description here...', :rows => 5 %>
                    </div>
                    <div class="form-group">
                      <%= f.label :price %>
                      <%= f.text_field :price, :class => 'form-control', :placeholder => '0.0' %>
                    </div>

                   <div class="form-group">
                      <%= f.label :display %>
                      <%= f.file_field :display %>
                    </div>
                    <div class="checkbox">
                      <label>
                        <%= f.check_box :status %> Is enabled?
                      </label>
                    </div>
                  </div><!-- /.box-body -->

                  <div class="box-footer">
                    <%= f.submit :class => "btn btn-primary" %>
                  </div>
                <% end %>

Model

class Product < ActiveRecord::Base


    has_attached_file :display, :styles => {:thumb => "500x500>"}
    validates_attachment_content_type :display, :content_type => /\Aimage\/.*\Z/

    scope :enabled, lambda {where("products.status = 1")}
    scope :sort_by_name, lambda { order("products.name asc")}
end
4
  • have you install Image Magick ...? Commented May 12, 2015 at 17:03
  • I think it's not installed. I couldn't find any gem for windows. Do you know any link for ImageMagick gem? Commented May 12, 2015 at 17:07
  • Image Magick is not a gem it requires a system wide installation look at it imagemagick.org/script/binary-releases.php Commented May 12, 2015 at 17:09
  • It's installed now. But giving the same issue. Commented May 12, 2015 at 17:37

1 Answer 1

2

You need to install ImageMagik in your machine

Install ImageMagik

And for windows 7+

Install File.exe

Sign up to request clarification or add additional context in comments.

6 Comments

It's installed and giving the same issue.
Did you restart the webserver?
Yes I did. Do we need to install any gem to use ImageMagik?
You need to provide the imagemagik command path. Check the windows part: github.com/thoughtbot/paperclip/blob/master/README.md
It worked now! I was missing the installation of file.exe for windows. Thanks a lot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.