Uploading multiple files with paperclip

后端 未结 1 369
难免孤独
难免孤独 2020-11-30 07:49

I am having problems uploading multiple files using paper clip,

my models are as such

slider has_many imgarrays

imgarrays has_many imageobjects

im         


        
相关标签:
1条回答
  • 2020-11-30 08:53

    Here is my code that worked well to upload multiple file using paperclip: We can achieve using nested attributes or using normal easy method.

    The following code shows normal method:

    User.rb

    has_many :images, :dependent => :destroy

    Image.rb

    has_attached_file :avatar, :styles => { :medium => "300x300>" }

    belongs_to :user

    users/views/new.html.erb

    <%= form_for @user, :html => { :multipart => true } do |f| %>
    
    ...... 
     ....
    
    <%= file_field_tag :avatar, multiple: true %>
    
    <% end  %>
    

    Users_controller:

    .....

        if @user.save
         # params[:avatar] will be an array.
         # you can check total number of photos selected using params[:avatar].count
          params[:avatar].each do |picture|      
        
            @user.images.create(:avatar=> picture)
            # Don't forget to mention :avatar(field name)
        
          end
        end
    

    Thats it. images got uploaded, this may not be the good way but it works.

    0 讨论(0)
提交回复
热议问题