Using Asciidoctor in Rails - ruby-on-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.

Related

Ahoy on Rails — is it supposed to track automatically?

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.

NameError in WelcomeController#index on RubyOnRails

I got this error
NameError in WelcomeController#index
uninitialized constant YouTubeIt
Rails.root: C:/Sites/rails_code/youtube
Btw i checked the gem author github, checked sintax of constant, seems everything's alright, so i can't figure it out.
There is a very similar question here:
Uninitialized constant SO
But in his case, there was a typo, i don't have any typo, i even checked the tutorial like 6 times, but there's no apparent error on my side, here's my controller code:
class WelcomeController < ApplicationController
def index
#cliente = ::YouTubeIt::Client.new(:dev_key => "AI39si4Ao5BFsYIkbzko7b9A_iktB2Pc8DAblJJ_JzJx6IL6Mju1dYYkMKY6TByz8MJPXfm4__tCAt9Is6Mvjg2JM55kuJVVqQ")
#videos = #cliente.videos_by(user: "AlbertoMaso2")
end
end
already installed the youtube_it and declared it's presence in the app by adding it to the Gemfile.
I'm stuck on this one and can't get it to work.
Anyone can shed some light upon this?
Thanks in advance!
When you have changed Gemfile, or added a new gem to it, please, make sure that you have updated Gemfile.lock also. To update lock file just run bundle install command.
Then in order to check usability the gem, you could not run whole rails server, but simply rails console with loaded development environment. Do it as follows:
$ rails c
or
$ rails console
Then inside the IRB, you can try your new gem features, in your case as follows:
irb(main):001:0> require 'youtube_it'
=> true
irb(main):002:0> YouTubeIt
=> YouTubeIt
What happens if you change
#cliente = ::YouTubeIt::Client.new...
to
#cliente = YouTubeIt::Client.new...

Object not recognized in Rails controller

I have this code in one of my controllers to get access to eventful.
eventful = Eventful::API.new('my_key_here')
However, I get the following error when I refresh my page
uninitialized constant LocationsController::Eventful
I installed the gem version 2.2.1 as directed through http://api.eventful.com/libs and my gemfile contains the line
gem 'eventfulapi', '2.2.1'
I am trying to recreate the example here: http://api.eventful.com/libs/ruby/doc/index.html
I can get it to work in a separate ruby script (e.x. running 'ruby a.rb' through command line) which leads me to thinking it's totally possible. I just can't get it to work in a controller to feed my application.
Try adding require 'eventful/api' at the top of the controller code.

Cannot find gem in controller

I'm sure this is an easy question but I'm having a hard time figuring out what to Google.
I'm trying to use the library ChunkyPNG.
I added it to my Gemfile and did a bundle install.
bundle list | grep "chunky"
* chunky_png (1.2.5)
So far so good.
I try using it in my controller:
image = ChunkyPNG::Canvas.from_data_url(params[:data]).to_image
(The docs for this method are available here)
It results in the following error:
NameError in MyController#create
uninitialized constant MyController::ChunkyPNG
Why is prepending the controller namespace? I imagine that's what is causing the error.
Otherwise, it means that ChunkyPNG is not install (and it clearly is).
Am I not able to use this gem upfront without writing some sort of rails plugin to wrap around it?
Thanks
EDIT:
Question has been answered, see #apneadiving's comment
In your controller, or somewhere else in the app do:
require 'chunky_png'

How can I override a method in a ConnectionAdapter class in Rails 3 for use in a rake task?

In order to override the table_exists? method in the Rails PostgreSQL adapter I have tried the following in an initializer file:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
def table_exists?(name)
raise 'got here'
end
end
This will raise the the following error:
uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
I believe this would have worked in previous versions of rails and I even found a small plugin that did something like this in Rails 2.3.6. Also I only encounter this error when I am trying to run a rake task like db:migrate and not when I start my application server.
Could someone show me the right way to do this and / or explain why PostgreSQLAdapter doesn't seem to be loaded when I am in an initializer file?
Instead of config/initializers, place that code in lib/ folder.
While this means that the active_record is loaded after the rails initializers, which is unusual. I ll update this with more detail, once I am done investigating the whole flow. If you want some more details about the rails 3 initialization process, check out this link:
http://ryanbigg.com/guides/initialization.html
I had success by moving this code into a Rails plugin. It is a little bit more overhead, but it is working consistently when I run rails s and when I run rake db:migrate.
I just followed the rails guide page on the topic and ran
rails generate plugin rails_patches --with-generator
and moved my init.rb file into rails as recommended.
~vendor/
`~plugins/
`~rails_patches/
|~lib/
| `-rails_patches.rb
|~rails/
| `-init.rb
|+test/
|-install.rb
|-MIT-LICENSE
|-Rakefile
|-README
`-uninstall.rb
I put this code in init.rb:
require 'rails_patches'
I put this code in rails_patches.rb:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
def table_exists?(name)
raise 'got here'
end
end
This now behaves as I expected.

Resources