图像上传使用回形针在ruby轨道上

时间:2015-05-12 17:01:37

标签: ruby-on-rails ruby ruby-on-rails-4

我尝试使用回形针上传图片。但是例外" Shop :: ProductsController#中的NoMethodError创建了未定义的方法`chomp'为零:NilClass "发生。任何人都可以看到并突出问题吗?

这是Gemfile代码

gem 'paperclip', '~> 4.2.1'

控制器

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

查看

<%= 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 %>

模型

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

1 个答案:

答案 0 :(得分:2)

您需要在机器中安装ImageMagik

Install ImageMagik

对于Windows 7+

Install File.exe