无法查看上载的图像

时间:2014-07-26 05:28:07

标签: ruby-on-rails image paperclip polymorphism

我正在尝试上传多个图片并在页面上显示。

我相信我已将它们上传,但我无法显示它们。

我搜索过并搜索过。想弄清楚如何显示这些图像。

通过回形针上传。

这是我的编码:

正如您所见,我的图像表中有图像

Image.last
  Image Load (34.6ms)  SELECT  "images".* FROM "images"   ORDER BY "images"."id" DESC LIMIT 1
 => #<Image id: 16, car_id: nil, created_at: "2014-07-23 05:32:47", updated_at: "2014-07-23 05:32:47", image_file_name: "15416359_large.jpg", image_content_type: "image/jpeg", image_file_size: 65742, imageable_id: 2, imageable_type: "Car"> 

表示部分编辑和新编辑。

<%= form_for(@car, :html => {:multipart => true}) do |f| %>
  <% if @car.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@car.errors.count, "error") %> prohibited this car from being saved:</h2>

      <ul>
      <% @car.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
    <div class="newPaperclipFiles">

      <%= f.fields_for :images do |image| %>

          <% if image.object.new_record? %>
              <%= image.file_field :image %>
          <% end %>

      <% end %>

    </div>

    <div class="existingPaperclipFiles">

      <% f.fields_for :images do |image| %>

          <% unless image.object.new_record? %>

              <div class="thumbnail">
                <%= link_to( image_tag(image.object.image.url(:thumb)), image.object.image.url(:original) ) %>

              </div

          <% end %>

      <% end %>
    </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

显示视图

<% fields_for :images do |image| %>

    <% unless image.object.new_record? %>

        <div class="thumbnail">
          <%= link_to( image_tag(image.object.image.url(:thumb)), image.object.image.url(:original) ) %>

        </div

    <% end %>

<% end %>

<%= link_to 'Edit', edit_car_path(@car) %> |
<%= link_to 'Back', cars_path %>

图像模型

class Image < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
  has_attached_file :image,
                    :styles => {
                        :thumb=> "200x200#",
                        :small  => "300x300>",
                        :large => "600x600>"
                    }
  validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end

汽车模型

class Car < ActiveRecord::Base
  has_many :images, as: :imageable, :dependent => :destroy
  accepts_nested_attributes_for :images, :allow_destroy => true
end

汽车控制器

  def show
    @car = Car.find(params[:id])
    10.times { @car.images.build }
  end

  # GET /cars/new
  def new
    @car = Car.new
    10.times { @car.images.build }
  end

  # GET /cars/1/edit
  def edit
    @car = Car.find(params[:id])
    10.times { @car.images.build }
  end

DB Schema

  create_table "cars", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "imageable_id"
    t.string   "imageable_type"
  end

 add_index "cars", ["imageable_id", "imageable_type"], name: "index_cars_on_imageable_id_and_imageable_type", using: :btree

  create_table "images", force: true do |t|
    t.integer  "car_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.integer  "imageable_id"
    t.string   "imageable_type"
  end

  add_index "images", ["imageable_id", "imageable_type"], name: "index_images_on_imageable_id_and_imageable_type", using: :btree

回答

<% @car.images.each do | image | %>
  <%= link_to image_tag(image.image.url(:thumb)), image.image.url(:original) %>
<% end %>

1 个答案:

答案 0 :(得分:0)

显示

如果您无法在show操作中看到图片,那么简单的解释就是您正在调用fields_for(这是因为没有显示而没有显示而臭名昭着任何例外):

<% fields_for :images do |image| %>

    <% unless image.object.new_record? %>

        <div class="thumbnail">
          <%= link_to( image_tag(image.object.image.url(:thumb)), image.object.image.url(:original) ) %>

        </div

    <% end %>

<% end %>

这对你不起作用 - 你会更好地使用以下内容:

#app/controllers/cars_controller.rb
Class CarsController < ApplicationController
   def show
      @car = Car.find params[:id]
   end
end

#app/views/cars/show.html.erb
<%= link_to image_tag(@car.images.first.image.url(:thumb)), @car.images.first.image.url(:original) %>

<强>语法

看起来你的Rails的语法有问题,你需要纠正

如果要显示图像,只需使用image_tag帮助程序即可。您目前正在尝试使用form辅助方法来显示图片,这只会导致您遇到的问题

解决这个问题的方法是单独使用image_tag助手:

<%= image_tag "your_path_to_the_image" %>

我绝对建议您留意研究Ruby / Rails语法的方法,以便您能够正确创建图像