簡體   English   中英

在 ruby​​ on rails 中使用回形針上傳圖像

[英]Image upload using paperclip in ruby on rails

我正在嘗試使用回形針上傳圖像。 但是出現異常“ NoMethodError in Shop::ProductsController#create undefined method `chomp' for nil: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

你需要在你的機器上安裝 ImageMagik

安裝 ImageMagik

對於 Windows 7+

安裝文件.exe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM