使用Paperclip上传多个图像 - Rails 4 App

时间:2014-11-08 22:47:16

标签: ruby-on-rails image paperclip image-uploading

我在这里经历了很多教程和问题,但似乎没有人帮助我 - 这可能是因为我对此不熟悉。最接近的是here,但它仍然没有帮助我解决这个问题。

我正在尝试使用回形针上传多张图片。 (我也有mini_magick gem)我已经成功使用这个gem创建了一个“Profile Image”部分,但现在我需要一个包含多个图片的部分。任何帮助都会很棒。目前,我在产品展示视图中上传图片时也没有显示图片,也没有保存到数据库中的产品。

产品表架构

 create_table "products", force: true do |t|
    t.string   "name"
    t.text     "description"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.date     "releasing_on"
    t.string   "website"
    t.string   "image_file_name"
    t.string   "industry"
    t.string   "slug"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.string   "company"
    t.string   "picture_file_name"
    t.string   "picture_content_type"
    t.integer  "picture_file_size"
    t.datetime "picture_updated_at"
 end

(产品控制器)products_controller.rb

class ProductsController < ApplicationController
  before_action :require_signin
  before_action :require_admin, except: [:index, :show]
  before_action :set_product, only: [:show, :edit, :update, :destroy]

def show
  @clockers = @product.clockers
  @categories = @product.categories

  if current_user
  @current_clock = current_user.clocks.find_by(product_id: @product.id)
  end
end

def edit
end

def update

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

def new
  @product = Product.new
end

private

    def product_params
  params.require(:product).permit(:slug, :name, :description, :price, :releasing_on, :website, :company, :image, :image_content_type, :picture, :picture_content_type, category_ids: [])
end

  def set_product
    @product = Product.find_by!(slug: params[:id])
  end

  end

(产品型号)product.rb

class Product < ActiveRecord::Base
before_validation :generate_slug

...    

has_attached_file :image

has_attached_file :picture

...

products.helper.rb(产品助手)

module ProductsHelper

...

def image_for(product)
  if product.image.exists?
    image_tag(product.image.url)
  else
    image_tag('placeholder_sneaker.png')
  end
end

def picture_for(product)
  if product.picture.exists?
    image_tag(product.picture.url)
  else
    ""
  end
end

end

产品 - show.html.erb(我想在哪里展示图片)

<h1> <%= @product.name %></h1> 

<div class="container">
<div class="row">
  <div class="col-md-4 thumb">
    <div class="thumbnail" > 
    <img class="img-responsive"> <%= image_for(@product) %>      <%# profile image %>
       <div> 
       <%= picture_for(@product) %>     <%# multiple pictures %>   
       </div>        
    </div>
</div>

_form.html.erb(产品展示/编辑/更新表格)

<%= form_for(@product) do |f| %>

  <%= render "shared/errors", object: @product %>


<form class="form-horizontal" role="form">
<div class="form-group">
    <ol>
      <li class=> 
          <%= f.label :image, "Profile Image" %>
          <%= f.file_field :image %>
      </li>
      <li class=> 
          <%= f.label :picture, "More Pictures" %>
          <%= f.file_field :picture, multiple: true %>
      </li>
    </ol>

0 个答案:

没有答案