Ahoy on Rails — is it supposed to track automatically? - ruby-on-rails

I've set up Ahoy in Rails 5.1.5 but it doesn't track visits automatically.
From https://github.com/ankane/ahoy:
Add this line to your application’s Gemfile:
gem 'ahoy_matey'
And run:
bundle install
rails generate ahoy:install
rails db:migrate
Restart your web server, open a page in your browser, and a visit will be created 🎉
I interpret that last line as: An entry will automatically appear in Ahoy::Visit.last when there's a new visit. Unfortunately, there were no rows in this table after a pageview. So I did this in application_controller.rb:
after_action :ahoy_track
private
def ahoy_track
properties = request.path_parameters
properties[:url] = request.url
ahoy.track "Pageload", properties
end
and now I'm getting visitor data. However, when someone comes from Google, Ahoy::Visit.last.search_keyword is nil.
Are these two problems related? What gives? This is deployed to Heroku on a plain vanilla PostgreSQL setup.

Related

Ruby on Rails themoviedb-api gem isn't recognized by app

I'm crating movie/series focused app and I decided to use this gem: https://github.com/18Months/themoviedb-api#configuration. I added it to my Gemfile, then I used 'bundle install' command, the gem was successfully installed. So far I have done everything by the book. After installation I tried to check if it works properly so I insert this code into one of my controllers' index actions:
def index
#series = Series.all
Tmdb::Api.key("my_api_key")
Tmdb::Movie.detail(550, language: 'it')
end
Unfortunately my app doesn't seem to recognize Tmdb api, and I am getting the error:
uninitialized constant SeriesController::Tmdb

Fixing rails migration ran on server instead of git repo using Capistrano

I upgraded Spree Commerce from version 3.0 to version 3.1 but forgot to check in the migration files on Git from my local development environment
I instead generated the migration files on the server instead; I ended up committing the migrations from my development environment to git but now I'm having all sorts of problems with deploying because it's trying to run the migration when the tables exists.
I guess I don't really need the migrations to run since it's on the server?
Running rake db:migrate:status on the server shows:
up 20151015124064 Add meta title to page.spree static content
up 20151015124065 Add render as partial for layout for spree pages.spree static content
up 20151015124066 Add pages stores.spree static content
down 20160707102753 Create spree store credits.spree
down 20160707102754 Create spree store credit categories.spree
down 20160707102755 Create spree store credit events.spree
down 20160707102756 Create spree store credit types.spree
down 20160707102757 Add missing indexes.spree
down 20160707102758 Remove duplicated indexes from multi columns.spree
down 20160707102759 Remove user index from spree state changes.spree
down 20160707102760 Add position to spree payment methods.spree
down 20160707102761 Add taxable adjustment total to line item.spree
down 20160707102762 Migrate payment methods display.spree
down 20160707102763 Spree payment method store credits.spree
down 20160707102764 Rename has and belongs to associations to model names.spree
down 20160707102765 Spree store credit types.spree
down 20160707102766 Add discontinued to products and variants.spree
down 20160707102767 Remove shipping method id from spree orders.spree
down 20160707102768 Add id column to earlier habtm tables.spree
down 20160707102769 Add indexes.spree
down 20160707102770 Add missing indices on user.spree auth
down 20160707102771 Remove show in footer from spree pages.spree static content
On my location machine it shows:
up 20151015124064 Add meta title to page.spree static content
up 20151015124065 Add render as partial for layout for spree pages.spree static content
up 20151015124066 Add pages stores.spree static content
up 20160707102753 Create spree store credits.spree
up 20160707102754 Create spree store credit categories.spree
up 20160707102755 Create spree store credit events.spree
up 20160707102756 Create spree store credit types.spree
up 20160707102757 Add missing indexes.spree
up 20160707102758 Remove duplicated indexes from multi columns.spree
up 20160707102759 Remove user index from spree state changes.spree
up 20160707102760 Add position to spree payment methods.spree
up 20160707102761 Add taxable adjustment total to line item.spree
up 20160707102762 Migrate payment methods display.spree
up 20160707102763 Spree payment method store credits.spree
up 20160707102764 Rename has and belongs to associations to model names.spree
up 20160707102765 Spree store credit types.spree
up 20160707102766 Add discontinued to products and variants.spree
up 20160707102767 Remove shipping method id from spree orders.spree
up 20160707102768 Add id column to earlier habtm tables.spree
up 20160707102769 Add indexes.spree
up 20160707102770 Add missing indices on user.spree auth
up 20160707102771 Remove show in footer from spree pages.spree static content
I presume the migration status on the server should be up not down.
Is there any tips how I should approach this?
If you don't have any data to lose you can drop those tables from your sql console and re-run capistrano deployment or you can manually up the migration file from the server by
rake db:migrate:up VERSION=20151015124064
where version is second column of the result of rake db:migrate:status
Note: remember you have to drop those tables on both cases
If you do not want to lose data and do it all over again there is an another approach that you can try.
A migration is marked as up or down based on whether migration version exists as a record within the schema_migrations table.
So, one way you can fix your issue is to add a file app/models/schema_migration.rb containing the following:
class SchemaMigration < ActiveRecord::Base
self.primary_key = :version
attr_accessible :version
# you can call the method below via console or even call
# or execute the commands directly from the rails console
def self.fix_migrations
# basically a list of all migrations that you run on server but are not marked as up
down_migrations = %w(20160707102753 20160707102754 ... 20160707102771)
down_migrations.each do |m|
# this will add an entry in the schema_migrations datatable
# on server so rake db:migrate won't try to run these again
SchemaMigration.create(version: m)
end
end
end
And then via rails console, you execute: SchemaMigration.fix_migrations.
In case you need to run a specific migration again or if you accidentally added a migration version that has never been executed before, you can always remove the entry from schema_migrations using SchemaMigration.find_by_version('xxxx').delete. This will allow rake db:migrate to try to run that migration again.

Using Asciidoctor in Rails

i tried to use asciidoctor gem in my rails app. I added it to my Gemfile and made bundle install.
Now i try to use asciidoctor within a Controller:
def show
#article.text = Asciidoctor.render(#article.text)
end
But i get an error:
uninitialized constant ArticlesController::Asciidoctor
Whats the right way to user asciidoctor-gem with rails?
Please remember to restart your server after changing something outside the Rails auto-reloading path (i.e. app/* and config/routes.rb).
Since the documentation looks exactly like your example
puts Asciidoctor.render '*This* is http://asciidoc.org[AsciiDoc]!'
I'd guess, you simply forgot to restart the server.

How do I call the Rails console's reload! command programmatically?

When using the Rails console, there's a handy reload! function which reloads models and such. How do I call this from another part of my program?
Edit I've been asked for the use case a bit. It was that I had a long running JVM process running Jruby and a clojure repl. I could run RSpec tests from the REPL and could run arbitrary Ruby code from there too. But I couldn't figure out how to reload the Ruby classes so that I could edit the code and see it changed in the Ruby runtime. I no longer use this setup, principally because testing it was such a pain.
(I'm using Jruby and can access the Ruby VM programatically from my backend).
Have you tried touching restart.txt? Unfortunately, I have no experience with JRuby, but confirmed it works on my app.
FileUtils.touch('tmp/restart.txt')
You probably want to do something other than a Get request, and secure it behind some authentication.
I threw it in an Admin controller and added the route to config/routes.
# app/controllers/admin.rb
class AdminController < ApplicationController::Base
##time = Time.now # This value gets cached with the model.
def reboot
FileUtils.touch('tmp/restart.txt')
#restarted_time = ##time
end
end
# config/routes.rb
namespace :admin
get 'reboot'
end
# app/views/admin/reboot.html.erb
<%= #restarted_time.to_s %>

My rails 3 engine's controller is only found by rails every second page refresh, otherwise returns LoadError

i'm developing a rails 3 engine but really having troubles getting the
controller to load every time.
every second time I visit the page I get;
LoadError in Webedit/public filesController#index
Expected /home/anko/.rvm/gems/ruby-1.9.2-p136/bundler/gems/webedit-3e02394235c3/app/controllers/public_files_controller.rb
to define PublicFilesController
to reproduce (assuming bash, ruby 1.9.2 and rails 3);
rails new webedit-test
cd webedit-test
echo "gem 'webedit', :git => 'https://github.com/ankopainting/webedit.git', :tag => 'v0.0.3'" >> Gemfile
bundle install
rails server
then goto http://localhost:3000/public
it will either say "hi" or an error.. refresh to see it change to the
opposite behaviour.
any help would be greatly appreciated.. I've spent some time in ruby
debugger but need to understand a lot about how rails works to get a
meaningful result.
I used the source code you provided and added a directory under controllers. Seems to work fine now. Since you have the controller inside a module, you need to create this directory structure:
app/controllers/webedit/public_files_controller.rb
Not exactly sure why it was loading every other time, though.

Resources