3

I am building an API with Ruby on Rails.

I have Users that have avatars implemented using paperclip.

I am trying to access my paperclip URL's in my JSON output and it just crashed my heroku instance. This works perfectly locally.

Snippet of My User Model

class User < ActiveRecord::Base

  has_attached_file :avatar, styles: { large: "600x600#", medium: "300x300#", thumb: "100x100#" }, default_url: "https://s3.amazonaws.com/whiztutor-marketing/photos/Profile300.png"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

end

Snippet of My Jbuilder for my user

json.cache! @user do
  json.id @user.id
  json.type @user.type
  json.f_name @user.f_name
  json.about @user.about
  json.email @user.email
  json.avatar_url @user.avatar.url(:medium)
  json.referral_code @user.referral_code
  json.education_level @user.education_level
  json.photo_url @user.avatar.to_json
  json.education_details @user.education_details.all
  json.all_subjects @user.subjects.all
  json.all_times @user.all_available_times.each do |time|
    json.day time.schedule.to_s
    json.time_block time.time_as_string
    json.next_occurrence time.schedule.next_occurrence(Time.now)
  end 
end

I tried wrapping this into a method as found on this question and it breaks the server the exact same way. I can even run console and access these URL's directly with the server. Something with JBUILDER and PAPERCLIP just don't mix and I can't seem to get to the bottom of it. Any help is greatly appreciated.

Community
  • 1
  • 1
JohnSalzarulo
  • 1,046
  • 10
  • 16
  • 1
    Can you post the error message you're seeing on Heroku? – Paul A Jungwirth Mar 13 '16 at 04:22
  • There could be an issue with the aws credentials configuration with paperclip on your server. Could you show us the error message and backtrace? – Rohit Jangid Mar 14 '16 at 07:56
  • I'm not getting any discernible error in my logs. I know the AWS credentials are good because I can retrieve all of the url's in my server console, not just my localhost console. Also, I can upload photos with no errors using postman and base64 encoding. These images all show up no problem within the webapp front end of the server side app. It's specifically retrieving the url's for JBUILDER that is having issues. – JohnSalzarulo Mar 14 '16 at 21:21
  • Cosole and server environment are entirely different. There could be a possible scenario that Heroku server is not initializing the environment variables. Could you mention where are you keeping aws credential. In `yaml` or `rb` file or you are using `ENV`? – Rohit Jangid Mar 15 '16 at 13:11
  • I am keeping variables using ENV - I know they are right on my server because in my server console, not LOCALHOST I can upload images and return the URL's. A live webapp running heroku is able to return the URL's with no issues. It's only when trying to render those URL's to JSON on that same server that things break down. – JohnSalzarulo Mar 15 '16 at 20:26
  • If there is no error, why do you say "JBUILDER is having issues"? What kind of issues? – Paul A Jungwirth Mar 16 '16 at 00:04
  • Postman says my heroku endpoint is giving a 500 server error. But my server logs show no issues. – JohnSalzarulo Mar 16 '16 at 20:33
  • Server logs from heroku simply read `2016-03-16T20:41:09.337047+00:00 heroku[router]: at=info method=GET path="/v1/tutors/56" host=whiztutorapi.herokuapp.com request_id=79a0123e-0a16-40ed-85aa-ae75b97f586a fwd="159.83.196.1" dyno=web.1 connect=1ms service=157ms status=500 bytes=330` as if it's a successful GET – JohnSalzarulo Mar 16 '16 at 20:42
  • I'm pretty sure it has something to do with the serialization paperclip does on that avatar field. – JohnSalzarulo Mar 16 '16 at 20:44

3 Answers3

2

Finally diagnosed this issue after several hours of research.

The issue is with the "json.cache! @user do" - Specifically the .cache! - I wanted to save a few server cycles and speed things up so this is how I implemented things at first.

Regardless, when I updated my JBUILDER code to the following I am no longer getting 500 server errors.

Snippet of My working Jbuilder for my user

json.id @user.id
json.type @user.type
json.f_name @user.f_name
json.about @user.about
json.email @user.email
json.small_photo @user.profile_url_small
json.medium_photo @user.profile_url_medium
json.referral_code @user.referral_code
json.education_level @user.education_level
json.education_details @user.education_details.all
json.all_subjects @user.subjects.all
json.all_times @user.all_available_times.each do |time|
    json.day time.schedule.to_s
    json.time_block time.time_as_string
  json.next_occurrence time.schedule.next_occurrence(Time.now)
end 
JohnSalzarulo
  • 1,046
  • 10
  • 16
1

It's hard to say what the problem might be without knowing the error, but these things look suspicious or just wrong:

json.avatar_url @user.avatar.url(:medium)
json.photo_url @user.avatar.to_json

The second one is going to give you extra quotes you probably don't want. And why have both?

Also here:

json.all_times @user.all_available_times.each do |time|
  json.day time.schedule.to_s
  json.time_block time.time_as_string
  json.next_occurrence time.schedule.next_occurrence(Time.now)
end 

I think you want this:

json.all_times @user.all_available_times do |time|
  json.day time.schedule.to_s
  json.time_block time.time_as_string
  json.next_occurrence time.schedule.next_occurrence(Time.now)
end 
Paul A Jungwirth
  • 23,504
  • 14
  • 74
  • 93
1

You can try this to get the url in json response, it works for me placed this method in your model.

def avatar_url
  avatar.url(:medium)
end

And call this method from controller,by overriding to_json()

render :json => @friends.to_json(:methods => [:avatar_url])
Hard Developer
  • 309
  • 1
  • 3
  • 12