Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to save a photo into DB via AngularJS in Rails4. I used paperclip gem for the attachment setup

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_attached_file :photo, :styles => {:thumb => "100x100>"}
end

Here's my form:

<div ng-controller="RegistrationsController">
  <form ng-submit="saveUser()" name="signUpForm" novalidate>
    <input type="text" class="form-control" ng-model="user.email" ng-class="{submitted: submitted}" required />
    <input type="file" ng-model="user.photo" />
  </form>
</div>

Here's my angularJS Controller. In saveUser() function it will trigger the POST request. I've tried to output the value of $scope.user.photo but it's undefined according to browser console log.

myApp.controller "RegistrationsController", ($scope, $location, $http, $modal)->

  $scope.saveUser = () ->
    console.log $scope.user.photo
    $scope.submitted = true
    if $scope.signUpForm.$valid
        $http(
            url: "/api/users"
            method: "POST"
            data: $scope.user
        ).success((data) ->
            $scope.user = {}

            $location.path "/"
            return
        ).error (response) ->
            console.log(response)
            return

        return

I don't have idea on how to save/upload a single photo via AngularJS + Paperclip in Rails.

And on how the value will be saved.

Here's the controller for the create method:

class Api::V1::UsersController < ApplicationController
  def create
    @user = User.new(trusted_params)

    if @user.save
      UserMailer.welcome_email(@user).deliver!
      render json: @user
    else
      render json: {
        message: 'Validation failed', errors: @user.errors.full_messages
      }, status: 422
    end
  end

  private

    def trusted_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :photo)
    end
end

Any ideas will be appreciated. Thanks!

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.