I want to create a download by uploading images via a web or ios application, I got an example https://github.com/defish16/ios-to-rails-images and I use Rails 4
Model:
class Test < ActiveRecord::Base
attr_accessor :avatar_data
has_attached_file :avatar, :styles => { medium: ["300x300>", :png], thumb: ["100x100>", :png]}
belongs_to :project
before_save :decode_avatar_data
def decode_avatar_data
if self.avatar_data.present?
data = StringIO.new(Base64.decode64(self.avatar_data))
data.class.class_eval {attr_accessor :original_filename, :content_type}
data.original_filename = self.id.to_s + ".png"
data.content_type = "image/png"
self.avatar = data
end
end
end
class Project < ActiveRecord::Base
has_many :avatars, class_name: 'Test', dependent: :destroy
accepts_nested_attributes_for :avatars
end
controller
class ProjectsController < ApplicationController
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render action: 'show', status: :created, location: @project }
else
format.html { render action: 'new' }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
def project_params
params.require(:project).permit(:name, :description, avatars_attributes: [:avatar])
end
class TestsController < ApplicationController
def create
@test = Test.new(test_params)
respond_to do |format|
if @test.save
format.html { redirect_to @test, notice: 'Test was successfully created.' }
format.json { render action: 'show', status: :created, location: @test }
else
format.html { render action: 'new' }
format.json { render json: @test.errors, status: :unprocessable_entity }
end
end
end
def test_params
params.require(:test).permit(:project, :avatar_data, :avatar)
end
I can get the normal download via the internet, but when I try to download via the ios app, I cannot get the image
here is some of my magazines
when I used the ios application Parameters: {"avatars_attributes" => [{"avatar_data" => "/ 9j / 4AAQSkZJRgABAQAAAQABAAD / 4QBYR .......
: { "project" = > { "name" = > "test3", "description" = > "teset3", "avatars_attributes" = > { "0" = > { "avatar" = > #
? Cuz app avatars_attributes "", ?
, ?