title: rails 4 backend and ios image upload
body: <p>I want to build a image upload buy can via web or ios app,I got a example <a href="https://github.com/defish16/ios-to-rails-images" rel="nofollow">https://github.com/defish16/ios-to-rails-images</a> , and I use Rails 4</p>
  
  <p>Model:</p>
  
  <pre><code>class Test &lt; ActiveRecord::Base
  attr_accessor :avatar_data
  
  has_attached_file :avatar, :styles =&gt; { medium: ["300x300&gt;", :png], thumb: ["100x100&gt;", :png]}
  belongs_to :project
  before_save :decode_avatar_data
  
  def decode_avatar_data
  # If avatar_data is present, it means that we were sent an image over
  # JSON and it needs to be decoded.  After decoding, the image is processed
  # normally via Paperclip.
   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 &lt; ActiveRecord::Base
  has_many :avatars, class_name: 'Test', dependent: :destroy
  accepts_nested_attributes_for :avatars
  end
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>class ProjectsController &lt; 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 &lt; 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
  </code></pre>
  
  <p>I can get a normal upload via web, but when I try to upload via ios app I can't get the image</p>
  
  <p>here is part of my logs </p>
  
  <p>when I used ios app
  Parameters: {"avatars_attributes"=>[{"avatar_data"=>"/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYR.......</p>
  
  <p>but in web
  Parameters: {"project"=>{"name"=>"test3", "description"=>"teset3", "avatars_attributes"=>{"0"=>{"avatar"=>#
  
  <p>Is it a problem?Cuz in app avatars_attributes didn't in the "project", how can I fix it?in app or server
  If not, Where would be a mistake??</p>