Why would I get "Errno::ENOENT: No such file or directory" when viewing a Sinatra form? - ruby

I am trying to follow this tutorial:
http://net.tutsplus.com/tutorials/ruby/singing-with-sinatra/
Got stuck in "We’ll also make use of a “view file”, which allows us to split the markup for a view into a separate file. "
I have my basics.rb file running fine.
And My files are stored as follows:
Desktop/RubyForm/basics.rb
Desktop/RubyForm/view/form.erb
However, now when i go to http://localhost:9393/form , I am greeted with:
Errno::EIO at /form
Input/output error - <STDERR> file: lint.rb location: write line: 398
sinatra.error
Errno::ENOENT: No such file or directory -
/Users/HelenasMac/Desktop/views/form.erb
UPDATE! : Got the form to work right after running ruby basics.rb and going to http://localhost:4567/form .
However, after I run "shotgun basics.rb" , I have to go to
http://localhost:9393/form, and that's when the form doesn't show up.
What am I doing wrong? Disclaimer: mega beginner to ruby and using the terminal.
Thanks in advance!

If you cannot get shotgun to work then the new recommended way to reload Sinatra seems to be rerun.
To use it:
> gem install rerun
> cd /Users/HelenasMac/Desktop/RubyForm
> rerun ruby basics.rb

Explicity Set a Views Directory
Unless you're using inline template for your views with enable :inline_templates, you may need to explicitly define a template directory if the default values aren't working for you. The docs describe how to set your views directory as follows:
:views - view template directory
A string specifying the directory where view templates are located. By default, this is assumed to be a directory named “views” within the application’s root directory (see the :root setting). The best way to specify an alternative directory name within the root of the application is to use a deferred value that references the :root setting:
set :views, Proc.new { File.join(root, "templates") }
You may also need to explicitly set :root, and make sure that both :root and :views make sense from your current working directory.

Related

Ruby including files

I have a Ruby app that runs on a server with no web interface. It is run using the command line(ruby path/to/file.rb).
I have classes in different files that I want to be accessible. The files are located in the "app/classes" directory.
I put this in the application.rb file:
config.autoload_paths += Dir["#{config.root}/classes"]
and I get an uninitialized constant error.
I can put in "require_relitive 'somefile'" but I would rather not have to do this for every class that is used. How do I create an autoload path and where should it be located at?
Use require_all
See https://github.com/jarmo/require_all
It basically allows you to write this:
require 'require_all'
require_all 'app/classes'
And all ruby files in app/classes will be loaded.

Middleman Output Path

EDIT for clarity:
I'm wondering if it is possible to have set an output path for files in a Middleman build. For organizational purposes I want to group a type of page into a folder to keep it out of the main source directory. However on build/server I would like it to render to a different path:
/source
index.html
/landingpages
landingpage1.html
landingpage2.html
I have :directory_indexes enabled in my config file would like to be able to have the files in landingpage output to the root directory:
/build
index.html
/landingpage1
index.html
/landingpage2
index.html
Is this possible to achieve this somehow using the config.rb file and still show up properly in the sitemap? I would prefer to not have to do this using .htaccess
Thanks
A technique I used in a current project is based around proxies and should also solve your case:
landingpage_templates = Dir['source/landingpages/*.html']
landingpage_templates.map! do |tpl_name|
tpl_name = File.basename(tpl_name).gsub(/.html$/, '')
proxy "/#{tpl_name}/index.html", "/landingpages/#{tpl_name}.html", :ignore => true
end
You should be able to do something like that:
page "/file1/index.html", :proxy => "/somefolder/file1.html"
page "/file2/index.html", :proxy => "/somefolder/file2.html"
I think you're better off using directory indexes instead though and organising your files like:
/source
index.html
file1.html
file2.html
In your config.rb
activate :directory_indexes

Change the default folder where temporary profile is created by firefox

I am getting Errno::ENOSPC error while trying to run watir tests headlessly on firefox. Actually the cause of this error is that I am logged it from a non-root user and when I run my tests, it tries to create directory for temporary firefox profile in 'tmp' folder. Because it doesn't use 'sudo', it is giving this error.
If I do 'mkdir xyz' in 'tmp', it gives 'No space in device' error, which is same as above.
How can I change the default profile folder (which is '/tmp') where webdriver is trying to create temporary profile? I want webdriver to create the temporary profile by itself but in the folder which I can set.
I am using Linux, ruby 1.9.2p320, selenium-webdriver 2.26.0 and watir-webdriver 0.6.1.
Appreciate your help!
I believe you have to monkey patch selenium-webdriver, as there does not appear to be a built in way to specify the directory that includes the temporary profiles.
Background
Seleium-webdriver (and therefore watir-webdriver) creates the temporary Firefox profile directory in \selenium-webdriver-2.26.0\lib\selenium\webdriver\firefox\profile.rb using the method:
def layout_on_disk
profile_dir = #model ? create_tmp_copy(#model) : Dir.mktmpdir("webdriver-profile")
FileReaper << profile_dir
install_extensions(profile_dir)
delete_lock_files(profile_dir)
delete_extensions_cache(profile_dir)
update_user_prefs_in(profile_dir)
profile_dir
end
The temporary folder is created by the:
Dir.mktmpdir("webdriver-profile")
Which is defined in the tmpdir library. For the Dir.mktmpdir method, the second optional parameter defines the parent folder (ie where to create the temporary profile). If no value is specified, as in this case, the temporary folder is created in Dir.tmpdir, which in your situation is the 'tmp' folder.
Solution
To change where the temporary folder is created, you can monkey patch the layout_on_disk method to specify your desired directory in the call to Dir.mktmpdir. It would look like:
require 'watir-webdriver'
module Selenium
module WebDriver
module Firefox
class Profile
def layout_on_disk
#In the below line, replace 'your/desired/path' with
# the location of where you want the temporary profiles
profile_dir = #model ?
create_tmp_copy(#model) :
Dir.mktmpdir("webdriver-profile", 'your/desired/path')
FileReaper << profile_dir
install_extensions(profile_dir)
delete_lock_files(profile_dir)
delete_extensions_cache(profile_dir)
update_user_prefs_in(profile_dir)
profile_dir
end
end
end
end
end
browser = Watir::Browser.new :ff
#=> The temporary directory will be created in 'your/desired/path'.
Setting the environment variable TMPDIR is enough to make ruby, and selenium, write their temporary files where you want.
From http://ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html :
Dir.tmpdir‘s return value might come from environment variables (e.g. $TMPDIR).
So, if you're in your shell:
export TMPDIR="/whatever/you/want/"

Ruby separate large source files into multiple files

I am writing a Ruby script which was supposed to be a small thing but has grown quite large, way to large to have everything crammed into one source file. So I am trying to separate the project into different files. I have four classes and I want to put each in its own separate source file.
What I did:
I moved all of the classes into their own files so now I have this
proj/GoogleChart.rb
proj/BarChart.rb
proj/PieChart.rb
proj/GroupedBarChart.rb
Now that they are in other files I am getting uninitialized constant GoogleChart (NameError) in all of my subclasses on the line where I inherit from GoogleChart, i.e.
require 'GoogleChart'
BarChart < GoogleChart
Can anyone tell me what is wrong?
Thanks
EDIT
Using ruby version 1.8.4
Also I have tried using the absolute path:
require 'C:/Documents and Settings/proj/GoogleChart.rb' and this is still producing a NameError
In Ruby 1.8.x, the . is part of your load path. So you should at least try to debug that by including something like:
puts $:
require 'GoogleChart'
class BarChart < GoogleChart
end
and load that in an IRB session:
Open the session in your directory proj.
Enter there require 'BarChart'
Look at the result.
For me it is:
c:\apps\ruby\test\proj>irb
irb(main):001:0> require 'BarChart'
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby/1.8/i386-msvcrt
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby/1.8/i386-msvcrt
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/1.8/i386-mingw32
.
=> true
So the require is successful for me, and the . is part of the path (as it should). As you can see, I am working with Ruby 1.8.7, I don't know if anything has changed since 1.8.4 that is relevant here.
So please describe exactly how you run your file:
Have you opened a shell to run the file?
What is the current working directory of that shell?
Do you run by double-clicking it?
It only works when you are in your proj directory and run there (with ruby in your shell path) ruby BarChart.rb.

How do I require a file from inside a directory with Ruby?

I think I'm missing something here. I have a directory like this:
myapp
|-lib
|-package1
|-dostuff.rb
|-package2
|-dostuff.rb
From an irb console I'm trying to test the library before I add it to my real project (a Rails app). However, typing this:
require 'lib/package1/dostuff'
returns an error saying it can't find the file to load. I added the lib directory to the load path but I'm not able to load the file.
What am I forgetting? The two filenames don't have to be the same but that's how they are to begin with (they are auto-generated from some web services I need to call using soap4r; each package represents a different web service API group)
If the directory "lib" is in the load path, the argument to require must be relative to lib. So require 'package1/dostuff' without the lib, otherwise it will look for lib/lib/package1/dostuff.rb.
In Ruby 1.9 there's the new require_relative method, which would let you do require_relative "../package2/dostuff" from within package1/dostuff.rb.

Resources