0

我有一个 has_many 图像关联,我尝试在一个表单中上传多个图像,但是当我单击提交按钮时我无法看到关联的属性。它是bug has_many images和的简单关联。images belongs_to bug我正在使用jQuery-File-Upload进行文件上传.关联如图所示:-

我的第一个模型

    class Bug < ActiveRecord::Base    
    has_many :bug_images, :dependent => :destroy
    accepts_nested_attributes_for :bug_images, :allow_destroy => true
    attr_accessible :bug_images_attributes

    has_attached_file :bug_image,
                    :styles => { :thumb => "75x75>", :small => "150x150>" },
                    :url => '/:class/:id/:attachment?style=:style'
end

我的第二个模型

  class BugImage < ActiveRecord::Base

   belongs_to :bug

    def image_file=(input_data)
    self.name = input_data.original_filename
    self.image_type = input_data.content_type.chomp
    self.size = File.new(input_data).size
   end

end

我的表定义

    class CreateBugImages < ActiveRecord::Migration
  def self.up
    create_table :bug_images do |t|
      t.string :name
      t.references :bug 
      t.string :image_type
      t.integer :image_size

      t.timestamps
    end
  end

  def self.down
    drop_table :bug_images
  end
end

##我的视图页面,我可以上传多个文件,但提交时无法获取上传的图像

  <% form_for(@bug, :url => "/myapp/bugs/create",:html=>{:multipart => true,:id=>"form_bug"}) do |f| %>
         <div class="row-fluid">
        <label class="FormLabel">Severity <font color="red">*</font></label> 
           %= f.select :sevearity,Bug::Severity_Type ,{:prompt => "Select    Severity                  Level"},:class=>'selectpicker'} %>       </div>        
        <div class="row-fluid">
        <label class="FormLabel">Attribute<font color="red">*</font></label>
        <%= select_tag 'attribute_id', options_for_select(@attributes.collect{ |u| [u.attribute_name,  u.id]}.insert(0, "")), :class=>'chzn-select',:required=>:true %>
       </div>

<!--  here i upload multiple images for a bug ->       
         <% f.fields_for :bug_images do |builder| %>
            <%= builder.file_field :image_file %>
          <% end %>
          <%end%>

我的服务器日志看不到 bug_images_attributes

    "---------in create--------------"
"bug"=>{"sevearity"=>"S1"}, "attribute_id"=>"7", "controller"=>"bugs", "action"=>"create", "HTTP_START_TIME"=>20
13-10-08 18:56:01 +0530}
">>>>>>>>>>>>>>>> application_api"
4

1 回答 1

0

如果你想将多个图像附加到一个 bug,bug_image 模型应该有 has_attached_file 声明。

class Bug
  has_many :bug_images
end

class BugImage
  belongs_to :bug
  has_attached_file :bug_image
end
于 2013-10-08T16:09:59.020 回答