简体   繁体   English

在 ruby​​ on rails 中使用回形针上传图像

[英]Image upload using paperclip in ruby on rails

I'm trying to upload image using paperclip.我正在尝试使用回形针上传图像。 But the exception " NoMethodError in Shop::ProductsController#create undefined method `chomp' for nil:NilClass " occurs.但是出现异常“ NoMethodError in Shop::ProductsController#create undefined method `chomp' for nil:NilClass ”。 Can anyone see and highlight the problem please?任何人都可以看到并突出显示问题吗?

Here is Gemfile code这是 Gemfile 代码

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

You need to install ImageMagik in your machine你需要在你的机器上安装 ImageMagik

Install ImageMagik安装 ImageMagik

And for windows 7+对于 Windows 7+

Install File.exe安装文件.exe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM