将两个参数传递给输入类型文件上传

时间:2015-04-30 11:16:12

标签: ruby-on-rails ruby-on-rails-4 carrierwave image-uploading

在我的场景中,我正在使用CarrierWave上传图片。但我的“图像”模型是多态的,因为不止一个模型使用“图像”模型。对于每个型号,有4种不同的图像类型(图标,标题,常规,滑块)。所以我将在“图像”模型中为它​​们制作“ENUM”。但问题是,在“新”或“编辑”视图中,我怎么知道哪个文件上传哪个“图像类型”?

所以这是我的图像模型:

class Image < ActiveRecord::Base
    belongs_to :imageable, polymorphic: true

    mount_uploader :image, ImageUploader

    enum image_type: {icon: 1, title: 2, regular: 3, slider: 4}
end

这是服务模型(此模型是具有图像的模型之一):

class Service < ActiveRecord::Base
    has_many :images, as: :imageable, :dependent => :destroy
    accepts_nested_attributes_for :images

end

这是我的_form.html.erb:

<div class="control-group">
   <!-- <label class="control-label" for="input1">Adı</label> -->
   <div class="controls">
      <%= f.input :service_name, input_html: { class: "span6", id: "input1"}, label: "Hizmet Adı" %>
   </div>
</div>
<div class="control-group">
   <!-- <label class="control-label" for="inputRemarks">Adres</label> -->
   <div class="controls">
      <%= f.input :service_description, input_html: { class: "span12 wysihtml5", rows: "6"}, as: :text, label: "Hizmet Açıklaması" %>

   </div>
</div>
<div class="control-group">
   <----- THIS PART IS FOR FILE UPLOAD ----------->
   <div class="controls">
      <label class="control-label">Hizmet Büyük Resimi</label> 
      <div class="fileupload fileupload-new" data-provides="fileupload">
        <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;"><img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt=""/></div>
        <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
        <div>
          <span class="btn btn-file"><span class="fileupload-new">Resim Seç</span><span class="fileupload-exists">Değiştir</span>
          <%= f.input :images, input_html: { type: :file, class: "default"}, wrapper: false, label: false %></span>
          <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Sil</a>
        </div>
      </div>

      <----- THIS PART IS FOR FILE UPLOAD ----------->
   </div>
</div>

<div class="control-group">
   <!-- <label class="control-label" for="input2">Soyadı</label> -->
   <div class="controls">
      <%= f.input :meta_keywords, input_html: { class: "span6", id: "input2"}, label: "Anahtar Kelimeler" %>
   </div>
</div>


<div class="form-actions">
  <%= button_tag(type: 'submit', class: "btn btn-primary") do %>
     Kaydet
  <% end %>


   <!-- <button type="button" class="btn">Cancel</button> -->
</div>

我是铁杆的新手,所以难道与否,我还不知道。但这种解决方案对我来说很好看。但是我坚持这个问题,我需要你的帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,因为我说我对Rails很新,并且在Rails中有一个“fields_for”表单帮助器。因此,对于嵌套属性,您可以使用“fields_for”表单帮助程序来执行此操作。在我的场景中,我添加了隐藏的字段。