Using http_accept_language gem on rails 3 - ruby-on-rails

I'm trying to use this gem to determine the user's preferred language, and running into some trouble.
undefined local variable or method http_accept_language for #<HomeController:0x964f5ec>
I included the gem in the Gemfile, ran bundle install, and restarted the server multiple times. Why doesn't my app recognize the gem?
Also, in my ApplicationController I wrote the following method:
def set_i18n_locale
http_accept_language.user_preferred_languages
available = %w{en kr}
params[:locale] = http_accept_language.preferred_language_from(available)
if params[:locale]
I18n.locale = params[:locale]
end
end
One thing I don't understand is the second line,
http_accept_language.user_preferred_languages
From https://github.com/iain/http_accept_language, this is supposed to return a sorted array. I thought I had to store the array into some variable and use it, but the author just throws the method like that. How does that work? Can't I just do the following?
available = %w{en kr}
params[:locale] = http_accept_language.language_region_compatible_from(available)
I am just a little confused by the author's explanation.
Thank you for your help.
UPDATE:
the gem, http_accept_language, doesn't seem to be installed successfully. It's on the gem list, but when I try to uninstall it, the error message shows that it's not installed. Why does this happen?
max#max-VirtualBox:~/appe$ gem list
*** LOCAL GEMS ***
...
http_accept_language (1.0.2)
...
max#max-VirtualBox:~/app$ sudo gem uninstall http_accept_language
INFO: gem "http_accept_language" is not installed

Try using request.user_preferred_languages instead of http_accept_language.user_preferred_languages.

In the documentation states that since version 2.0, the gem is a Rack middleware, but the problem is that the only 2.0 version released in June 2012 is only a pre-release. Therefore, to get the version 2.0 you need to do this:
gem 'http_accept_language', '~> 2.0.0.pre'

I didn't have to use the gem to implement the feature I wanted.
def set_i18n_locale
unless params[:locale]
params[:locale] = extract_locale_from_accept_language_header
end
available = ['en', 'kr']
if available.include? params[:locale]
I18n.locale = params[:locale]
end
end
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
def default_url_options
{ :locale => I18n.locale }
end
Official Guide and Agile Development helped a lot.

As for me solution was to apply #DouweM PR from the github. Here is the line from the Gemfile:
gem 'http_accept_language', :git => 'https://github.com/DouweM/http_accept_language', :branch => 'no-middleware-no-crash'

put in your Gemfile:
gem 'http_accept_language', '~> 2.0.0.pre'
and then in the code use env aka:
env.http_accept_language
works for me.
% bundle show|grep acc
* http_accept_language (2.0.0.pre)

Related

How to configure: Rails - Spree - i18n - /locale in url or default to / for default locale

I am trying to configure a heroku/rails/spree site to use locale in the url. For example: www.sample.com/en/products and www.sample.com/ca/products and default locale at: www.sample.com/
The spree i18n gem is working fine, allowing translations in spree backend. The i18n gem for rails is allowing for local change correctly via menu selector.
The site is in three languages. The default is Spanish, with options for English and Catalan.
initializers/locale.rb
# tell the I18n library where to find your translations
I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
# set default locale to something other than :en
I18n.default_locale = :es
Which works perfectly. However I would like to fix the admin backend language to english(en).
The i18n instructions for point to the routes configuration:
config/routes.rb
scope "(:locale)", locale: /en|nl/ do
resources :books
end
I have tried all kinds of combinations to get:
config/routes.rb
mount Spree::Core::Engine, at: '/'
To work with /en and /ca but without success.
I have tried to use the routing-filter gem to wrap the spree application in a locale, but with little success.
I thought that locales in url for multilingual sites was the preferred method. Believing that there would be support or a tutorial covering the subject. But my research has not found any solutions.
I can get /ca/ /es/ and /en/ to work. But I still need to get es to work at / instead of /es/.
Here is the current configuration:
application_controller:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :set_locale
def url_options
{ locale: I18n.locale }
end
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
Rails.application.routes.default_url_options[:locale]= I18n.locale
end
end
With routes as:
Rails.application.routes.draw do
scope "(:locale)", :locale => /en|es|ca/ do
mount Spree::Core::Engine, at: '/'
end
end
I was thinking it would need:
In the controller, look to the local or url to set locale correctly. If no locale or locale in url, set to default.
Then once the locale is set always, mount the spree engine on / is es, on /ca if ca, or on /en is en locale.
I tried the routing-filter gem. With filer locale in the routes. All it did was default the application to ca locale always. Even on /es and /en. Also the page rendered a fullpage into the template payload, which gave me two page headers. Something very wrong there.
I tried this approach: i18n Routing To Mounted Engine - Ignoring locale but no configuration worked for me.
Looks like the spree-globalization gem was not installed correctly. Now I have all the /locale/ paths working with not configuration in routes or the application controller. The only thing to do now is push the default locale to root /.
I found that the problems was the rails build itself. I seem to have this problem with rails from time to time. It gets to a point where it stops working correctly. Unfortunately I was not far enough along to have a stable branch.
So I rebuilt the rails fresh.
Added these gems:
gem 'spree', '~> 3.1.0.rc1'
gem 'spree_auth_devise', '~> 3.1.0.rc1'
gem 'spree_gateway', '~> 3.1.0.rc1'
gem 'spree_i18n', git: 'git://github.com/spree/spree_i18n.git', branch: '3-1-stable'
gem 'spree_reviews', github: 'spree-contrib/spree_reviews', branch: '3-1-stable'
gem 'spree_globalize', github: 'spree-contrib/spree_globalize', branch: 'master'
After some experimenting the above is the stable gem revisions for that combination.
One failing I got was the spree globalise gem would not install. So I manually copied the include statements for the vendor js and css and then ran:
rake spree_globalize:install:migrations
Then migrated. With the default locales set as per the documentation. The application default correctly and /en and /ca altered the locale and link pathing as intended.
Lesson learned.

Whois gem not working in rails

class DomaincheckerController < ApplicationController
def index
end
def store
r =Whois.whois(secure_params['domain'])
render :text => "#{r}"
end
private
def secure_params
params.require(:whois).permit(:domain)
end
end
This is my domainchecker controller. The index method renders a form. After submitting the form it goes to store method. Here I am trying to use the whois gem. I have installed whois gem by running gem install whois. But I am getting this error.
uninitialized constant DomaincheckerController::Whois
The problem is that you installed the gem directly and not using bundler, therefore the Rails app can't find the dependency.
In order to install a gem in a Rails project you need to edit the Gemfile file and add the gem there. Once added, run
$ bundle
in order to install the dependency. Check the documentation about the Gemfile.

numbers_and_words gem not working - NoMethodError in Posts#index - undefined method `to_words' for 42:Fixnum

I would like to convert some numbers in words.
So I installed the numbers_and_words gem with "gem install numbers_and_words" -
I restarted the server, and tried to run this example from the Read.me in my index.html.erb:
<%= 42.to_words %>
but I get this error:
NoMethodError in Posts#index -
undefined method `to_words' for 42:Fixnum
I checked the gem documentation a few times, but I couldn't figure out, what I am missing.
This is my posts controller regarding index.
def index
#posts = Post.order("created_at desc")
#published = Post.where(draft:false).order("created_at desc")
#drafts = Post.where(draft:true).order("created_at desc")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
What did I wrong? Did I forget something in the installation process?
I am quite new to rails, sorry if this is a trivial newbie question.
Thank you so much for your help! Really appreciated.
Installing a gem is not sufficient to make it available in a Rails project. You need to add it to the Gemfile so that you can manage the dependencies with Bundler.
Edit the 'Gemfile' and add the gem
gem 'numbers_and_words'
Then run bundle again to update the Gemfile.lock
$ bundler
This will make the gem available to the app. It will also autorequire the gem on boot, if the gem uses the standard naming conventions.
It seems this gem is name correctly. Otherwise, you can explicitly require it by setting a require option in the gemfile
gem 'numbers_and_words', require: 'numbers_and_words'
If you just installed the gem locally and you didn't configure the Gemfile, the application will crash once you will deploy it.
Found a solution - not mentioned in the documentation of the gem, but it did the trick for me:
I added
require 'numbers_and_words'
in the rake file. After server restart, it's working.

ruby on rails roo gem cannot load zip/zipfilesystem

I am attempting to use the roo gem to process .xlsx spreadsheets that are uploaded by an outside party. I'm getting the following error:
LoadError (cannot load such file -- zip/zipfilesystem):
I've found a lot of questions similar to this one (such as cannot load such file -- zip/zip) and I've tried to follow their solutions. So far, to no avail.
I originally required 'roo' in the controller, and after getting this error tried requiring 'zip/zip', 'zip/zipfilesystem', and just 'zip'. None of these seem to fix anything. I've also tried adding :require => 'zip', :require => 'zip/zipfilesystem', :require => 'zip/zip' to the Gemfile, and none of that seemed to change anything. Here is some pertinent code:
in Gemfile:
# for spreadsheet upload management
gem 'roo'
gem 'rubyzip'
gem 'spreadsheet'
gem 'nokogiri'
installed versions:
nokogiri (1.6.0)
roo (1.12.1)
rubyzip (1.0.0)
spreadsheet (0.8.9)
in Controller:
require 'roo'
module BatchOrderProcessing
class DataFilesController < ApplicationController
def create
# some code here ...
when ".xlsx"
spreadsheet = Roo::Excelx.new(uploaded_io.path, nil, :ignore)
header = spreadsheet.row(1)
if # some validation stuff...
puts "spreadsheet format inappropriate"
redirect_to # some place
end
process_datafile(fname, spreadsheet)
# more code ...
end
private
def process_datafile(fname, spreadsheet)
#df = DataFile.new
#df[:filename] = ActiveRecord::Base.connection.quote(fname)
if #df.save
begin
# parse asynchronously
datafile_scheduler = Rufus::Scheduler.new
datafile_scheduler.in '3s' do
#df.process_spreadsheet(spreadsheet)
end
redirect_to #df
rescue => e
# more code ...
end
else
# more code ...
end
end
I think this thing is crapping out before it gets to the model (where the process_spreadsheet() code is), but just in case, here's some model code:
def process_spreadsheet(spreadsheet)
# do some stuff
puts "parsing spreadsheet"
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
row_array << row
invoice << row.to_s
# some more code....
dfi = DataFileItem.new()
dfi.attributes = row.to_hash.slice(*accessible_attributes)
dfi.data_file_id = self.id
dfi.save
self.data_file_items << dfi
# Update stuff in our DB based on rows in row_array...
end
I'm using rails 3.2.13 and ruby 2.0.0p195.
Am I requiring the wrong thing (or in the wrong way) somewhere? Let me know if any other code snippets would be helpful. Thaaaaanks.
rubyzip v1.0.0 was released 29 August 2013: https://github.com/rubyzip/rubyzip/releases
This is a new major version number, and more than one gem or project that depends on this has been caught out by the break with backwards-compatibility.
The quickest "get my code working like before" fix is to alter Gemfile reference to rubyzip:
gem 'rubyzip', '< 1.0.0'
In the longer-term, this may not be the best fix, it depends on how and/or why you are using rubyzip. I expect some gem publishers such as roo's authors will need to figure out how to transition nicely so that their own users don't end up with simultaneous requirements for incompatible versions of rubyzip.
Just opinion:
Seeing this in action has actually made me much less a fan of Ruby gems semantic versioning for major versions. If I ever break with backwards compatibility on any of my own projects, I think I'll just start a new gem, and put a notice on the old gem.
Try adding the zip-zip gem to your project. It provides a simple adapter for your dependencies using the RubyZip v0.9.9 interface allowing you to upgrade to RubyZip v1.0.0.
This has been fixed in the roo gem. You need to update to version 1.12.2 or higher to fix. See the issue here: https://github.com/Empact/roo/pull/65

Why is this line breaking Rails with Passenger on DreamHost?

Ok, so I have a Rails app set up on DreamHost and I had it working a while ago and now it's broken. I don't know a lot about deployment environments or anything like that so please forgive my ignorance. Anyway, it looks like the app is crashing at this line in config/environment.rb:
require File.join(File.dirname(__FILE__), 'boot')
config/boot.rb is pretty much normal, but I'll include it here anyway.
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end
def booted?
defined? Rails::Initializer
end
def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end
def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end
def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end
def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end
class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end
class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end
class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end
def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end
class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end
def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end
def load_rubygems
require 'rubygems'
min_version = '1.1.1'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end
rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end
private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end
# All that for this:
Rails.boot!
Does anyone have any ideas? I am not getting any errors in the log or on the page.
-fREW
I had the same problem on DreamHost. Freezing rails and unpacking all gems got me past it.
rake rails:freeze:gems
rake gems:unpack:dependencies
My guess would be that you're breaking because of a newer version of the Rails gems on Dreamhost. At least, that's been my issue when things have blown up in something like boot.rb.
Try freezing the gems from your development environment into your vendor/rails directory.
Ya - the problem is not really in boot.rb - it's just that boot.rb is where rails is actually loaded.
So you'll get an error like this if you've specified a version of Rails that just doesn't exist on your dreamhost slice. This can happen if you either upgrade your project, start a new project (and forget that you upgraded rails in the meanwhile) or if you're still using an old version of rails and it's now been removed from the dreamhost server that you're on.
To figure out which is is, look in config/environment.rb for the line that'll read something like:
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
Then ssh to your dreamhost server and type gem list and see if your version is in the list.
If not, you an try several options. Lets say the version you're using is 2.3.4
To begin with, try: gem install rails -v=2.3.4 then restart. That may be all that is required.
If that doesn't work, then try freezing and unpacking the gems (as per the other answer here).
There is also another possibility - that you're actually missing a gem that rails depends on but which is failing silently - eg a dependency on a certain version of rack caught me out once. But you may also have other gem dependencies
If you run rake gems you will be able to list all the gems that your project knows about that it needs - make sure they're installed to begin with.
Then, as a sort of rough smoke-test, try running script/console - if you're missing an important rails gem, script/console won't load and should fail, giving you a notice about the gem you need.
Update:
If you're trying to run v 2.3.5, you may also be suffering from this problem:
Bypassing rack version error using Rails 2.3.5
In which case you'll need to follow the instructions there.

Resources