我已添加emdedly紅寶石寶石(https://github.com/embedly/embedly-ruby)到我的社會書籤應用。從網址中嵌入圖片,並將URL鏈接替換爲我應用程序中的圖片。它在當地完美運作,但它在生產(Heroku)中不起作用。我是編程新手,我不知道如何捕捉生產中的錯誤。我們歡迎所有的建議!嵌入式紅寶石寶石在本地工作,但沒有在生產
這裏是我的bookmark.rb模型文件:
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; [email protected])'
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
這裏是我的意見部分bookmark.html.erb文件:
<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>
,這裏是我的application.js文件我JavaScript的文件夾:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .
這裏是我的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'
請讓我知道是否有任何其他文件,你想看到。我也通過嵌入式教程http://embed.ly/docs/api/tutorials閱讀,但我沒有在我的代碼中使用jQuery來嵌入式地工作,正如它們在教程中使用的一樣。你認爲我需要在我的代碼中使用jQuery來使它工作嗎?但那麼爲什麼要在沒有jquery而沒有生產的情況下在本地嵌入工作呢?
謝謝@Sasha幫助我理解如何編寫關於該問題的更多具體信息!它不會在任何地方拋出任何錯誤。它正在打印工作鏈接,而不是使用嵌入圖像作爲鏈接。我發現問題是由於在我的應用程序中編寫了嵌入式代碼_after_我已經創建了書籤,因此書籤網址沒有被告知運行嵌入代碼來顯示圖像。通過執行以下操作修復了此問題:heroku run rails console,Bookmark.all.each do | bookmark |,bookmark.send(「set_embedly_url」),結束。 – 2014-09-29 16:59:40
酷!感謝更新! – Sasha 2014-09-29 18:35:41