2014-07-26 74 views
1

我想要獲取多個圖像以上載並顯示在頁面上。無法查看上傳的圖像

我相信我有他們上傳,但我無法顯示他們。

我已經搜索和搜索。試圖弄清楚如何讓這些圖像顯示出來。

通過回形針上傳。

這裏是我的編碼:

正如你可以看到我做的有圖像,我的形象表

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 %> 
+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的語法方式,讓你能夠創建正確的圖像

+0

我用你的上面的例子,它適用於第一張圖片。但我怎麼會通過所有的圖片做循環。而不必手動編碼每一個...一些可能有4個可能有10 –

+0

得到它謝謝... –