Rails 4: Multiple image upload using paperclip

ⅰ亾dé卋堺 提交于 2019-12-06 04:13:26

I see couple of problems in your code:

First thing, you need to remove the following line from Location model:

attr_accessor :asset, :assets_attributes

as its making asset and asset_attributes as virtual attributes which is why they are not saved in database. Also, you don't need asset attribute in Location model as its been taken care by Asset model.

Then, update the location_params as suggested by @Pavan:

def location_params
  ## Use `require` method
  params.require(:location).permit(:name, :notes, :longitude, :country, :latitude, :query, assets_attributes: [ :asset, :asset_content_type, :asset_file_name, :tempfile, :asset_file_size, :asset_updated_at, :_destroy])
end

Next, update the create action as below to ensure Locations are unique by name:

def create
  @location = Location.find_by(name: location_params[:name])
  unless @location
    @location = Location.new(location_params)
  end 
  respond_to do |format|
    if @location.save
      format.html { redirect_to @location, notice: ' <borat voice> Great success! </borat voice>' }
      format.json { render :show, status: :created, location: @location }
    else
      format.html { render :new }
      format.json { render json: @location.errors, status: :unprocessable_entity }
    end
  end
end 
Milind

try using <%= a.file_field :asset, :multiple=>"true",:name=>"location[assets][asset][]"%> for handling multiple uploads.

Hope it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!