2017-05-28 22 views
0

我想要顯示用戶在「顯示」頁面上上傳的每張圖像。閱讀文檔並觀看一些視頻,我可以創建上傳器並使其工作。 (也許這很明顯,但對我來說非常重要)。我期待Gem文檔,但沒有找到具體的。我嘗試對物體做一些操作,但沒有奏效。
這是我的模型如何使用載波列出對象的每個圖像

class Imovel < ApplicationRecord 
    belongs_to :bairro 
    belongs_to :tipoimovel 
    has_many :caracteristica_imovels, inverse_of: :imovel 
    has_many :caracteristicas, through: :caracteristica_imovels, dependent: :destroy 
    accepts_nested_attributes_for :caracteristica_imovels, reject_if: :all_blank, allow_destroy: true 
    mount_uploaders :imagens, ImagemUploader 
end 

這是上傳代碼:

class ImagemUploader < CarrierWave::Uploader::Base 
    storage :file 

    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

這是我_form以及它如何上傳

<%= f.file_field :imagens, multiple: true, class: "float-left" %> 

我,我通過顯示顯示頁這個:

<%= image_tag(@imovel.imagens[1].url) if @imovel.imagens? %> 

這裏是問題,我有數組索引,我不知道如何使它工作的動態。 我該怎麼做?

+0

只是循環儘管它與'@imovel.imagens.each做...' – max

+0

還限制自己的一個問題。不是兩個無關的問題。 – max

+0

已經嘗試做到這一點,但會拋出一個錯誤:未定義的方法'url'爲nil:NilClass。試試像這樣:<%@ imovel.imagens.each do | imagem | %> <%= image_tag(@ imagem.url)if @ imovel.imagens? %> <% end %>我錯過了什麼?對於這兩個問題抱歉,已經說得對,謝謝你的提問。 – SuBzer0

回答

1

在@max的幫助下,正確的方法是使用每種方法,正確執行。代號爲波紋管:

<% @imovel.imagens.each do |imagem| %> 
    <%= image_tag(imagem.url) %> 
<% end %> 
相關問題