Transfer JSON data to Heroku Rails application - ruby-on-rails

I am quite new to Rails and noticed that none of my data from my local environment was being pushed to the Heroku environment using the heroku run rake db:migrate command.In particular I was wondering how to transfer the JSON data to the Heroku environment.
Gem File:
source 'https://rubygems.org'
gem 'figaro'
gem 'angular-rails'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-script', '~> 2.2.0'
# See https://github.com/rails/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', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'rails_12factor', group: :production
end
/config/environments/production.rb:
Rails.application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
config.action_mailer.delivery_method = :smtp
end
Angular Controller:
myApp.controller("BlogController", function($scope, $http){
$http.get('/assets/blogs.json').success(function(data){
$scope.blogs = data;
String.prototype.trunc = String.prototype.trunc ||
function(n){
// this will return a substring and
// if its larger than 'n' then truncate and append '...' to the string and return it.
// if its less than 'n' then return the 'string'
return this.length>n ? this.substr(0,n-1)+'...' : this;
};
});
});
I am also receiving the following error in my console for the deployed environment: Error: Unknown provider: eProvider <- e

Changing the pattern of the AngularJs controller resolved the issue:
myApp.controller("BlogController", ['$scope', '$http', function($scope, $http){
$http.get('/assets/blogs.json').success(function(data){
$scope.blogs = data;
String.prototype.trunc = String.prototype.trunc ||
function(n){
// this will return a substring and
// if its larger than 'n' then truncate and append '...' to the string and return it.
// if its less than 'n' then return the 'string'
return this.length>n ? this.substr(0,n-1)+'...' : this;
};
});
}]);

Related

Coffeescript Not Updating in Heroku

So, I've made a few changes to my web app that's hosted on Heroku and then I did my usual
git add .
git commit -m
git push
git push heroku
There were no errors during any of those commands... But then when I check the website, it seems like all the changes got pushed except for the changes in my Coffeescript file. And when I look at the source code of the webpage, I see the old JS code... It's like it skipped the changes in the coffeescript file.
There were no errors in the push and the coffee script file was in the list of files when I did the commit.
Here's what I've tried so far:
Add to my gemfile
gem 'therubyracer'
Add to Production.rb:
config.assets.compile = true
config.assets.initialize_on_precompile = true
Restarted Heroku server
Deleted my browser data...
None of this worked. Someone please help or point me in the right direction.
This is my Gemfile:
source 'https://rubygems.org'
ruby "2.2.1"
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
gem 'bootstrap-sass', '~> 3.3.6'
gem 'sprockets-rails'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'gon'
group :development, :test do
gem 'byebug'
end
group :development do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'web-console', '~> 2.0'
gem 'spring'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '2.11.1'
gem 'therubyracer'
end
And this is production.rb File:
Rails.application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.initialize_on_precompile = true
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
end
Thanks!
you likely have an error in your js or css that is keeping your assets from compiling. try compiling locally by running:
rake assets:precompile
then add and commit again before re-deploying.

Mandrill API using Rails 4.2.5 Excon::Errors::SocketError

I am using Mandrill in my Rails app to send transactional e-mail. I use the 'mandrill-api' gem and I configured it according to the documentation.
It works fine on production but in local an error raides:
Unable to verify certificate, please set `Excon.defaults[:ssl_ca_path] = path_to_certs`,
`ENV['SSL_CERT_DIR'] = path_to_certs`, `Excon.defaults[:ssl_ca_file] = path_to_file`, `ENV['SSL_CERT_FILE'] = path_to_file`,
`Excon.defaults[:ssl_verify_callback] = callback` (see OpenSSL::SSL::SSLContext#verify_callback),
or `Excon.defaults[:ssl_verify_peer] = false` (less secure).
I don't understand why it is working on production and not in local?
Here is my config:
gemfile
source 'https://rubygems.org'
ruby '2.2.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/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', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
#gem 'byebug'
gem 'better_errors'
gem 'binding_of_caller'
gem 'quiet_assets'
gem 'pry-rails'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
group :production do
gem 'rails_12factor'
end
# Privacy
gem 'figaro'
# Heroku
gem 'heroku'
# Private access!
gem 'lockup'
# Mails
gem 'mandrill-api'
development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# Mails
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
# Devise
#config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
# TODO: do it with a template: http://blog.nvisium.com/2014/10/mandrill-devise-and-mailchimp-templates.html
config.action_mailer.smtp_settings = {
:address => 'smtp.mandrillapp.com',
:port => 587, # ports 587 and 2525 are also supported with STARTTLS
:enable_starttls_auto => true, # detects and uses STARTTLS
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_API_KEY'], # SMTP password is any valid API key
:authentication => 'login', # Mandrill supports 'plain' or 'login'
:domain => ENV['HOST'], # your domain to identify your server when connecting
}
end
mandrill.rb
require 'mandrill'
# Use an environment variable instead of placing the key in source code
MANDRILL = Mandrill::API.new ENV['MANDRILL_API_KEY'], true
Fonction raising the error only in local:
def send_short_email(subject, message, receiver)
inner_message = {
to: [{name: receiver.name, type: 'to', email: receiver.email}],
subject: subject,
html: message,
text: message,
from_email: ENV['DEFAULT_EMAIL_SENDER']
}
MANDRILL.messages.send inner_message
logger.debug inner_message
end
I tried changing settings, hosts or API key but nothing works. And there is no mention of this problem in the gem documentation.
Do you have an idea?
Thanks!
CĂ©line
As suggested by Casper in its comment, I edited my development.rb file to skip certificate verification.
development.rb
require 'excon'
Rails.application.configure do
...
Excon.defaults[:ssl_verify_peer] = false
end

Error in git push heroku master " Precompiling assets failed "

I'm trying to do a git push heroku master to deploy my project but happens the following error.
I'm following a tutorial however I have done what is asked many times and did not get success so I decided to ask for help
It happens the following error
Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
Psych::SyntaxError: (<unknown>): mapping values are not allowed in this context at line 15 column 11
/tmp/build_1ff5086696451e8482f0d9358917ef79/vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/application.rb:391:in `secrets'
/tmp/build_1ff5086696451e8482f0d9358917ef79/config/initializers/uploadbox.rb:16:in `block in <top (required)>'
/tmp/build_1ff5086696451e8482f0d9358917ef79/vendor/bundle/ruby/2.0.0/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:118:in `configure'
]
Tasks: TOP => environment
(See full trace by running task with --trace)
!
! Precompiling assets failed.
!
! Push rejected, failed to compile Ruby app
My Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 4.2.1'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.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', '~> 4.0.3'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks', '~> 2.5.3'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
#gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'simple_form', '~> 3.1.0'
gem 'slim-rails', '~> 3.0.1'
gem 'start', '~> 0.1.1'
gem 'devise', '~> 3.4.1'
gem 'uploadbox', '~> 0.2.0'
gem 'bourbon', '~> 4.2.2'
gem 'neat', '~> 1.7.2'
gem 'high_voltage', '~> 2.3.0'
gem 'normalize-rails', '~> 3.0.3'
gem 'better_errors', '~> 2.1.1'
gem 'binding_of_caller', '~> 0.7.2'
group :development do
gem 'xray-rails'
gem 'pry-rails'
gem 'sqlite3', '~> 1.3.8'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
# Use sqlite3 as the database for Active Record
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
File uploadbox.rb
Uploadbox.retina = true
Uploadbox.retina_quality = 30
Uploadbox.image_quality = 70
if Rails.env.production?
REDIS = Redis.connect(url: ENV["REDISCLOUD_URL"])
Resque.redis = REDIS
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
end
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: Rails.application.secrets.s3_key,
aws_secret_access_key: Rails.application.secrets.s3_secret
}
config.fog_directory = Rails.application.secrets.s3_bucket
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
if Rails.env.test?
CarrierWave.configure do |config|
config.storage :file
config.enable_processing = false
end
end

How come helper.distance_of_time_in_words_to_now Time.now works in development but not production

I can call
helper.distance_of_time_in_words_to_now Time.now in development and it works just fine, but for some reason in production I am now getting translation missing: en.datetime.distance_in_words.less_than_x_minutes
What can I do to fix this?
Here is my gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.5'
gem 'pg', '0.17.1'
gem 'sass-rails', '~> 4.0.3'
gem 'coffee-rails', '~> 4.0.1'
gem 'uglifier', '>= 2.5.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'
gem "bootstrap-sass", "~> 3.1.1.1"
gem "will_paginate", "~> 3.0.5"
gem "bootstrap-will_paginate", "~> 0.0.10"
gem "redcarpet", "~> 3.1.1"
# 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', '~> 2.0.7'
group :development, :test do
gem "foreman", "~> 0.63.0"
gem 'annotate', '~> 2.6.3'
gem 'rspec-rails', '~> 2.14.2'
gem "guard-rspec", "~> 4.2.3", :require => false
gem "factory_girl_rails", "~> 4.4.0"
gem "guard-spork", "~> 1.5.1"
gem "spork-rails", "~> 4.0.0"
# gem 'spork-rails', github: 'sporkrb/spork-rails'
end
group :test do
gem 'rb-fsevent', '~> 0.9.4', :require => false
gem "growl", "~> 1.0.3"
# # Test gems for Linux
# gem 'rb-inotify', '0.8.8'
# gem 'libnotify', '0.5.9'
# # Test gems for Windows
# gem 'rb-fchange', '0.0.5'
# gem 'rb-notifu', '0.0.4'
# gem 'win32console', '1.3.0'
end
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
gem 'dotenv-rails'
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
gem 'unicorn', '~> 4.8.2'
gem 'unicorn-worker-killer', "~> 0.4.2"
# Authentication
gem "devise", "~> 3.2.4"
gem "cancan", "~> 1.6.10"
gem "geocoder", "~> 1.2.0"
gem "carrierwave", "~> 0.10.0"
gem "mini_magick", "~> 3.7.0"
gem "fog", "~> 1.22.0"
gem "unf", "~> 0.1.3" # for use with the fog gem
gem "validate_url", "~> 0.2.0"
gem 'psych', '~> 2.0.5'
gem 'paper_trail', '~> 3.0.1'
gem 'faraday', '~> 0.9.0'
# Use Capistrano for deployment
group :development do
gem "capistrano", "~> 3.2.1"
gem 'capistrano-rbenv', '~> 2.0'
gem "capistrano-bundler", "~> 1.1.0"
gem "capistrano-rails", "~> 1.1.0"
end
# Use debugger
# gem 'debugger', group: [:development, :test]
And here if my production.rb:
Example::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both thread web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
# config.action_dispatch.rack_cache = true
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# Set to :debug to see everything in the log.
config.log_level = :info
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# config.assets.precompile += %w( search.js )
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found).
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
# For devise
config.action_mailer.default_url_options = { :host => 'example.com' }
end
I figured out the problem. i18n gem version 0.6.10 was yanked, but I was using that in my system. As soon as I rolled back to 0.6.9 everything worked fine.

config.consider_all_requests_local set to true without full trace

I'm working on a new app today this is my Gemfile :
source 'https://rubygems.org'
gem 'rails', '3.2.8'
# Static pages
gem 'high_voltage', '0.9.2'
# Dry Controller
gem 'coast'
# Light Views
gem 'slim'
gem 'formtastic'
# Uploading
gem 'paperclip'
# Authentication
gem 'devise'
gem 'cancan'
# Easing models
gem 'enumerize'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'twitter-bootstrap-rails', :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
group :test, :development do
gem 'sqlite3'
gem 'awesome_print'
# For slim genetaror instead default
gem 'slim-rails'
gem 'factory_girl_rails', '~> 4.0'
gem 'forgery'
end
gem 'jquery-rails'
And this an extract from my environment/development.rb :
config.cache_classes = false
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.action_dispatch.best_standards_support = :builtin
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
config.assets.compress = false
config.assets.debug = true
Slim::Engine.set_default_options pretty: true, sort_attrs: false
When I go to a page with an error for example this one :
p
= #project.envelopedd
I get a 500 page error instead of the normal debug trace. Any idea to fix this ?
This is a "bug" in the Rails documentation. I got burned by this just now also. There is a PR open on the Rails repo to address the docs issue.
The TL;DR: is that you need to implement show_detailed_exceptions? instead of local_request?

Resources