How to install Alchemist on Ruby on Rails - ruby-on-rails

How do I install the Alchemist gem?
I have added gem 'alchemist' to my Gemfile
bundle install
then I try 10.miles.to.kilometers and get an error: undefined methodmiles'`
according to alchemist I have to add Alchemist.setup somewhere. Where do I add this?

I don't know the gem, but you can try to put it in an initializer, ie in config/initializers/alchemist.rb:
Alchemist.setup
Restart your app after you have added this file.

In terms of your second problem. If you call to_f on an Alchemist::Measurement it gives you the value in terms of the base (in this case meters). If you want to get the number of kilometers you need to call value on it:
irb(main):005:0> 10.miles.to.kilometers.value
=> 16.09344
There might be a useful discussion on if I should change these if they are confusing.

Related

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.

undefined method `translate' for I18n:Module

On a server i had an installation of the gollum wiki. It ran fine. Now I also had to install redmine on that same server. This was a big pain, as redmine refused to run with puma. I had to mess around a lot with different gems to make it work at all.
But for some reason i now get an error from gollum, whenever i try to create a new wiki page:
NoMethodError at /create/old/git-tips
undefined method `translate' for I18n:Module
The stack trace shows that the error occurs in this line of stringex:
/var/lib/gems/1.9.1/gems/stringex-2.0.5/lib/stringex/localization/backend/i18n.rb in i18n_translations_for
::I18n.translate("stringex", :locale => locale, :default => {})
So i checked the installed packages with gem list and the required version of i18n 0.6.1 is there.
Any idea, what could be wrong and how to fix this?
For reference here's the output of gem list.
This may or may not help, but I had a similar problem on a large code base where there was an application-specific I18n module (which included some custom helper methods), but this was overriding the 't' method (a shortened form of translate).
In this case I found I could force the base-level I18n module by using two colons...
::I18n.t('thing')

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'

Debugging/Breakpoint-ing the Rails Core with Ruby-Debug?

How do I debug the rails gems? I've just found the beauty of ruby-debug and am real excited, but I want to step through the routing code in action_controller, but the debugger isn't set up at this time, it seems like it's just skipping over my "debugger" calls in action_controller/routing/route_set.rb for example.
What am I missing?
I just tested this with Rails 2.3.4. I added a 'debugger' line to the call method in vendor/rails/actionpack/lib/action_controller/routing/route_set.rb, ran 'rdebug script/server', browsed to a page, and it stopped at the correct line.
You can also use a class/method breakpoint; you'll need to step through the first few lines of the app until you're past the require 'action_controller' line, and then enter: b ActionController::Routing::RouteSet.call.
Or you can try setting a breakpoint based on the file name and line number.

Flickr gem : how to access user details?

https://github.com/ctagg/flickr/tree/master
I'm trying the example in the home page of the GEM Flickr.
require 'flickr'
flickr = Flickr.new('MY_KEY')
user = flickr.users('sco#scottraymond.net')
user.name
user.location
While I'm able to get the user object, I can't get any of the other attributes like name, location etc. How to get those details?
I worked through the problem I was experiencing above in my comment but I am not having difficulty getting people's attributes. The following code works as expected in a ruby script/console session:
require 'flickr'
f = Flickr.new('<<MY_KEY>>')
u = f.users('sco#scottraymond.net')
puts u.name
puts u.location
Substituting my username ('topherfangio') instead of Scott's, the name attribute works, but apparently my location is nil.
Could you describe a little bit more about exactly what you are getting? Any errors or is it just blank? Have you tried multiple users?
Edit 1: Just got your API key and it doesn't work for me either, try the following and see if that makes any difference. Also, are you using a non-commercial key or a commercial key?
f = Flickr.new(:api_key => 'MY_KEY', :shared_secret => 'MY_SECRET')
My guess is that it's not fully authenticating or something like that.
Edit 2: I'm a freaking liar...I was typing f = Flickr.new(...); f.name which was breaking. Your API key is working correctly for me. Perhaps it's your network or something?
To not entirely answer your question, I've been using the Fleakr gem and found it to be extremely straightforward.
Documentation is good and you do everything in the code once it is installed.
No tricks, just sudo gem install fleakr
It depends on the builder gem, so also do sudo gem install builder.
Once I switched to Fleakr my project finished quickly!
It looks like the flickr gem isn't being maintained. Uninstall the gem by doing a gem uninstall flickr, then install a more up to date branch of it by doing a gem install moonpxi-flickr

Resources