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
Related
I want to use the functions ".hour", ".minute",etc. in my rails model.
These are coming from following library: activesupport/lib/active_support/duration.rb.
What is the best way to access this in my model? The code is working in the rails console so I assume the file is available.
I tried doing the following in the top of my model (like mentioned in the documentation http://guides.rubyonrails.org/active_support_core_extensions.html):
require 'activesupport/lib/active_support/duration.rb'
and got this error:
cannot load such file -- activesupport/lib/active_support/duration.rb
It feels like such a noobish question but I can't seem to find the answer.
The Rails Guide recommends to use the following pattern (in combination with the specific date extension):
require 'active_support'
require 'active_support/core_ext/date/calculations'
I'm new to Ruby on Rails and I'm looking at an application that has a variable called current_teacher. I cannot seem to find where this is set. Everywhere I look the code seems to read from it but where is it set. Is this one of those things that Rails does for you. There is a mode and a table called teachers, so I'm sure this has something to do with it.
I'm very confused by statements like the following, can someone tell me how Rails does this?
if current_teacher.can_request_fieldtrip
Suppose you have a controller like :
class ClientsController < ApplicationController
def new
if current_teacher.can_request_fieldtrip
# code
end
end
end
Here is debugging tips :
(a) put this in your Gemfile and do bundle install :
`gem 'pry-rails', :group => :development`
(b) Put the line binding.pry just before the if statement.
(c) Start rails server using rails s.
(d) Hit the browser like http://localhost:3000/new
(e) Now you will be in the Pry console. Just do in the console,
method(:current_teacher).source_location
And the above line tell you where the method has been defined.
Documentation of Method#source_location
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native)
Rails does not support authentication by itself, however there are a lot of 'add-ons' that rails can use. These 'add-ons' are called gems. This can be a little confusing because you can't actually see their code inside your project folder.
If you open a file called "Gemfile" (it should be in your project folder) you can see a list of gems that you use. Try searching their names on google, you will probably find official web page that contains it's documentation. That way can learn what they do and how to use them.
current_teacher method smells like "Devise" gem
https://github.com/plataformatec/devise
I'm not sure about can_request_fieldtrip, this could be a custom method defined in Teacher model.
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
I am using Ruby on Rails 3.0.7 and I am writing some documentation for my application using RDoc. Since I have not found on the Web some good documentation with examples, what I would like to know is how to use the :include: directive at all.
Can you make me an example of using that in application files?
Here is the doc: http://rdoc.rubyforge.org/RDoc/Markup.html
And here is a very basic example:
First a ruby file, say /tests/my_func.rb
#:include: doc.txt
def my_function
puts "yo"
end
Then a doc /tests/documentations/doc.txt
This describes the method very well
In command line (executed from /tests):
rdoc -i /Users/benjaminroth/Sites/Tests/rdoc/descriptions
AIUI, :include: allows you to (surprise) include the contents of another file, keeping the same indentation level of the block in which the include appears.
It will look for the named file in the current directory, but it's something you can override by means of the --include switch.
If you want an example, this could prove useful.
On a Rails 3 app hosted at Heroku, where a multipart file is POSTed to my app, I'm trying to use some sample code that says :
File.open(params['filename'][:tempfile].path)
however, my logs show the error NoMethodErr no such method as tempfile.
I also tried
File.open(params[:filename].tempfile.path)
got the same error.
I added require 'tempfile' to my Controller, made no difference.
When a file is posted to your application, the object in the params should already be a Tempfile so calling [:tempfile] or .tempfile should not be necessary. Try something like this:
File.open params[:filename].path