rails 3 and rubyzip 1.0.0 uninitialized constant Zip::ZipFile - ruby-on-rails

Im attempting to open a docx file and write back into it using rubyzip 1.0.0 and rails 3.
In my gemfile I have:
gem 'rubyzip'
and the code i'm running is;
module Look
class Generator
def initialize(item)
doc = Nokogiri::XML.parse(item.to_xml)
xslt = Nokogiri::XSLT(File.read("<path_to_xslt_file>.xslt"))
#outxml=xslt.transform(doc)
zip = Zip::ZipFile.open("<path_to_docx_file>.docx")
#outxml
end
end
end
While the #outxml is created correctly (I can manually add it to the docx file and see the results), I can't even begin with creating the zip file because of this...
uninitialized constant Zip::ZipFile
Having checked all the documentation and tried many combinations I'm still completely stumped.
Can anyone please tell me why this won't work?
Thanks.

Just figured this one out by checking the latest documentation. Seems v1.0.0 was only released today so everything I read was out of date.
Anyway, the solution is to use
Zip::File.open

Related

Ruby on Rails - Roo gem | Issue opening active record attachment

Status:
I have a simple new Ruby On Rails App
I need to import some data from MS Excel in .xlsx format
I programmed an uploading to ActiveRecord as attachment via an attribute called 'excel'
I found a gem called Roo which should do the opening of the attachment
...via: Roo::Excelx.open() command
Then executes the accessing part of the file
Issue:
Roo::Excel.open() doesnt work for:
Roo::Excel.open(excel)
Roo::Excel.open(excel.attachment)
What is the correct command to execute to open the ActiveRecord::Attachment?
how about do this?
ModelName.excel.open do |file|
xlsx = Roo::Spreadsheet.open(file)
end
Actually I got an answer outside of Stackoverflow which was this:
Roo::Spreadsheet.open(ActiveStorage::Blob.service.path_for(excel.key), extension: 'xlsx')
I loaded one row from the Excel so must change code but opening it seemed to have worked! :-)

"No such file or directory # rb_sysopen" using OpenURI

I've recently run into a problem using OpenURI. Every open method results in the following error:
"No such file or directory # rb_sysopen".
My code looks simply like the following:
data = open("http://google.ca/")
I noticed the error shortly after adding gem 'nokogiri' to my Gemfile and running bundle install, though I have no indication of whether or not this caused the problem and have since removed the entry with no positive impact on the problem. Any help would be appreciated.
Try to write require 'open-uri' before your code.
I am using Ruby 3.0.1 and a part from the:
require "open-uri"
I have to explicitly call URI.open instead of just open:
data = URI.open("http://google.ca/")
Maybe it is something on new Ruby versions

Why uninitialized constant HomeController::HideMyAss?

Cant figure out what I'm doing wrong.
Using hidemyass gem for proxy.
Using github example code.
class HomeController < ApplicationController
require 'hidemyass'
def index
HideMyAss.options[:max_concurrency] = 3
response = HideMyAss.get("www.google.com", timeout: 2)
end
end
Whey I try to use them in my rails controller, I get the Uninitialized constant error.
uninitialized constant HomeController::HideMyAss
Tried looking at the source to figure out with no luck. Maybe it's the problem with my code. Gemfile is good and tried looking at all the things causing the problem.
There is no need to write require 'hidemyass'
You just need to add gem 'hidemyass' to Gemfile and do bundle install. Check installation with gem list hidemyass command. I have successfully checked it and used github example. It works fine and smoothly.

Custom error message for carrierwave 0.9.0 doesn't work for Rails 4

I use carrierwave 0.9.0 with Rails 4 and I'm trying to make a custom error message. After doing some search, I found this answer:
en:
errors:
messages:
extension_white_list_error: 'My Custom Message'
I tried this by going to config/locales/en.yml then adding the code above, but nothing changed. The error message is still the same.
There is also the same issue on github here, but no answers, I think this problem is specific just with Rails 4 but not sure, have you the same problem when you use Rails 4?
There seem be some loading issue with Rails 4.0 with regards to i18n files
Ideally what should be I18n load paths as per 3+
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activesupport-4.0.0/lib/active_support/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activemodel-4.0.0/lib/active_model/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activerecord-4.0.0/lib/active_record/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/actionpack-4.0.0/lib/action_view/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/carrierwave-0.9.0/lib/carrierwave/locale/en.yml
/Users/joshianiket22/carrierwave_tester/config/locales/en.yml
What is seen in Rails 4.0
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activesupport-3.2.11/lib/active_support/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activemodel-3.2.11/lib/active_model/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activerecord-3.2.11/lib/active_record/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/actionpack-3.2.11/lib/action_view/locale/en.yml
/Users/joshianiket22/workspace/zenjavi/carrierwave_tester/config/locales/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/carrierwave-0.9.0/lib/carrierwave/validations/../locale/en.yml
One can clear see the difference between the two the carrierwave en.yml is loaded after a the application specific en.yml and there is your issue
I suggest there is no easy way unless you the change the load_paths in rails application and some how manage to change the order of load_paths of as expected
I have given a pull request over here. Completely at awe of Carrierwave guys to decide on it
Hacky Solution :
I was refraining in giving you this solution earlier but still if you want it that bad here what you can do
define a file in lib directory(let say auto_load_i18n.rb) and assign the lib path to autoload (in application.rb)
config.autoload_paths += %w(#{config.root}/lib)
Inside auto_load_i18n.rb write this
I18n.load_path.delete(Rails.root.join("config/locales/en.yml").to_s)
I18n.load_path << Rails.root.join("config/locales/en.yml").to_s
And require the lib file at the top of your application_controller.rb
require 'auto_load_i18n'
class ApplicationController < ActionController::Base
and I guess everything would work then
You can now understand as to why I was refraining in giving this as a possible solution :)
Hope this help
It's fixed now guys:
https://github.com/carrierwaveuploader/carrierwave/pull/1264
Thanks for the patience.
I use a rails 5.1
I created a file config/locales/carrierwave.ar.yml and wrote
ar:
carrierwave:
errors:
messages:
min_size_error: "حجم الصورة لابد أن يكون أكبر من %{min_size}"
max_size_error: "حجم الصورة لابد أن يكون أقل من %{max_size}"
....
You can take a look at this file :)

Problems parsing a web-based XML file

I coded a little Ruby script that would parse a remote XML file and extract some data from it using Nokogiri. Now I'm trying to code a more advanced version as a Rails application.
I have my code inside of a controller. It's similar to the code that I used in my Ruby script, however it's not working. I believe the error is because it's trying to load the XML locally rather then externally.
Here is the error that Rails is giving me:
No such file or directory - http://mal-api.com/anime/10?format=xml
Here is a sample of the code in my controller: (I can provide the whole thing if needed, but it's mainly just the default Rails scaffold code.)
def create
require 'nokogiri'
#anime = Anime.new(params[:anime])
doc = Nokogiri::XML(open("http://mal-api.com/anime/#{#anime.mal_id}?format=xml"))
#Title
title = doc.css("anime english_title").inner_html
#Snipped rails scaffold code
end
mal_id is passed in through a form. Nokogiri is added in my Gemfile.
Is there something I'm missing or that I've done wrong?
Any help is appreciated.
By default the open method in ruby is used to open files. If you want to directly open an URL you need to require 'open-uri'. More information can be found in the docs: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI.html

Resources