我想要獲取多個圖像以上載並顯示在頁面上。無法查看上傳的圖像
我相信我有他們上傳,但我無法顯示他們。
我已經搜索和搜索。試圖弄清楚如何讓這些圖像顯示出來。
通過回形針上傳。
這裏是我的編碼:
正如你可以看到我做的有圖像,我的形象表
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模式
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 %>
什麼問題?它只是不顯示? –