1

I have added the emdedly-ruby gem (https://github.com/embedly/embedly-ruby) to my social bookmarking appliction. Embedly pulls in an image from a url and replaces the url link with the image in my application. It is working perfectly in local, but it doesn't work in production (Heroku). I am new to programming and I do not know how to capture an error in production. Any suggestions would be much appreciated!

Here is my bookmark.rb model file:

class Bookmark < ActiveRecord::Base
  #attr_accessible :thumbnail_url

  has_many :bookmark_topics
  has_many :topics, through: :bookmark_topics

  has_many :user_bookmarks
  has_many :users, through: :user_bookmarks

  has_many :likes, dependent: :destroy


 default_scope { order('updated_at DESC') }

 after_create :set_embedly_url

 private 

 def set_embedly_url
  embedly_api = Embedly::API.new :key => ENV['EMBEDLY_API_KEY'],
        :user_agent => 'Mozilla/5.0 (compatible; mytestapp/1.0; my@email.com)'

  Rails.logger.info ">>>> embedluy object: #{embedly_api.inspect}"

  obj = embedly_api.extract :url => self.url
  Rails.logger.info ">>>> embedly obj: #{obj.inspect}"
  # get result and find the attribute for the thumbnail_url
  # set the bookmark's thumbnail_url with that value -- update_attribute(thumbnail_url: val_of_thumbnail_url_from_response)
  o = obj.first
  Rails.logger.info ">>>> o: #{o.inspect}"
  image = o.images.first
  Rails.logger.info ">>>>> image: #{image.inspect}"

  update_attribute(:thumbnail_url, image['url'])
  Rails.logger.info ">>>> Now i am: #{self.inspect}"
  true
 end

end

Here is my bookmark.html.erb partial file in views:

<div class="btn-group"> 
<% show_remove_button ||= false %> 

<li>
  <div class="bookmark_container">
    <div class="thumbnail">
    <% link_text = bookmark.thumbnail_url.present? ? "<img src='#{bookmark.thumbnail_url}'/>".html_safe : bookmark.url %>
    <%= link_to link_text, bookmark.url %>  
    </div>  
  </div>
  <% if current_user %>
      <div class="like">      
        <% if show_remove_button %> 
          <% user_bookmark = get_user_bookmark_for(bookmark) %>
          <% if user_bookmark  %>
            <%= link_to "Remove", user_bookmark_path(user_bookmark), class: 'btn btn-danger', method: :delete %>
          <% end  %>
        <% else %>    
          <% if like = current_user.liked(bookmark) %>
            <%= link_to [bookmark, like], class: 'btn btn-info', method: :delete do %> Unlike <% end %>
          <% else %>
            <%= link_to [bookmark, Like.new], class: 'btn btn-info', method: :post do %> Like <% end %>
          <% end %> 
        <% end %>
      </div>
    <% end %>
</li>

and here is my application.js file in my javascript folder:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .

Here is my gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.5'


group :development, :test do
  gem 'rspec-rails'
  gem 'capybara'
end


 group :production do
   gem 'pg'
   gem 'rails_12factor'
 end

 group :development do
   gem 'sqlite3'
 end

gem 'bootstrap-sass', '~> 3.1.1'


# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.2'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

gem 'devise'
gem 'faker'
gem 'figaro'
gem 'embedly'

Please let me know if there are any other files you would like to see. I have also read through embedly tutorials at http://embed.ly/docs/api/tutorials, but I am not using jquery in my code to make embedly work, as they are using in the tutorials. Do you think I am required to use jquery in my code to make it work? But then why would embedly work in local without jquery and not in production?

Valerie Mettler
  • 475
  • 4
  • 16

1 Answers1

1

After a cursory look at your code, I can't tell for sure why it isn't working in Production, if it's working on local, but here are some debugging thoughts.

Firstly, try to be specific (in your own debugging and with SO), about what "not working" means. Is it throwing an error (where/when/how)? Is it printing a link with no embedded behavior? Is it printing the full Embedly link as an HTML string? Etc. This will help you/us narrow this down.

Once you have a sense for what is going wrong more precisely, here are some next steps for debugging:

  • If it's throwing an error, that's likely because a), you haven't included Embedly environment variables on Production that are necessary and included locally (such as the EMBEDLY_API_KEY), or some similar tool setup issue, or b), you haven't migrated the thumbnail_url column on Heroku, or something that causes a database issue.

In either case, you can probably best diagnose this issue by producing the error on production and then running heroku logs from your console to get a view of the most recent production server logs. You could also do heroku logs --tail to get a running log. These would help you see any errors, and also see, because of your puts statements, what is and isn't successfully run.

  • If it's not throwing an error, and not showing up, this might be a JS error.

To diagnose this, open up the JavaScript console in Web Inspector (CMD+Shift+J on a Mac, I believe) to see if there are any JS errors, either in loading files (your Embedly library, etc) or in your JS files themselves. This might have to do with how Heroku deals with the Asset Pipeline. If this is the case, check out the Rails Guide to the Asset Pipeline for some heavy/interesting reading.

  • The least likely of the cases that comes to mind is that it's just not HTML sanitizing or something, in which case you'd just see a blob of HTML on the page. In that case, I'd just Google the HTML sanitization method you're using and "production" or something, to see what else people have run into, or edit the question or start a new one, with the narrower error description.

Hope that helps!

Sasha
  • 6,224
  • 10
  • 55
  • 102
  • 1
    Thank you @Sasha for helping me understand how to write more specific information about the problem! It was not throwing any errors anywhere. It was printing a working link instead of using the embedly image as the link. I found out that the problem was due to having written the embedly code in my app _after_ I had already created bookmarks and therefore the bookmark urls hadn't been told to run the embedly code to display the image. This problem was fixed by doing the following: heroku run rails console, Bookmark.all.each do |bookmark|, bookmark.send("set_embedly_url"), end. – Valerie Mettler Sep 29 '14 at 16:59