All posts by rogerdpack

Ruby require in local directory

So it turns out that if you require a ruby file that is in your local directory, it doesn’t work. It loads the file and the class names, but leaves out all of the functions, etc.

To get it to work

instead of

require ‘localfile’

require ‘./localfile’ works.

Go figure.

Also note there is a discreapncy between require and load, which is interesting. load _requires_  the “.rb” (at least for me) while require does not. A minor inconsistency.

How to create headers or footers “system wide” for all pages in RoR

To create a header/footer “system wide” for all pages:

go to app\views\layouts
delete everything (unless you want a certain controller’s worth of pages to NOT have the header/footer), then create a file called “application.rhtml”
One way to do so is rename one of the old one’s (that you delete) as application.rthml. That will now serve as the “layout” for all views, into which file you can create your default header/footer.

If you want a generic header for each file (since we just deleted all the headers) put

 <title><%= controller.class %> <%= controller.action_name %></title>

in the layout and rename it application.rhtml and delete all the others and you have a perfect header/footer system.

Bug squashing: missing footer

Tip: So…find that suddenly your header and footer for your web page don’t work on certain pages, whereas in the rest they do (i.e. a few pages don’t show it?) What may have happened is that in your app\views\layouts you deleted the layouts for most of the pages, but left a few there (note typically to add a footera nd a header you delete the layout pages for each controller, and add an “application.rthml” so what really happened is you created a controller after completing this process, i.e. you created some “after” having deleted them all).

How to change the default “opening file” in Ruby on Rails

So you delete public\index.html file,

then in
map.connect '', :controller => "welcome"

Well… that tell it to connect the default page to the welcome controller’s default action, which is the “index” action:

def index
render :action => ‘list’

So if you so desire and do not have a “welcome” class
ruby script\generate controller welcome
then create (through some means) app/views/welcome/index.rhtml and voila you have a Ruby Index page now.

Said differently
If you don’t have a controller “welcome” (already generated) then you can either list a controller that you do have, or create the “welcome” controller thus:

ruby script\generate controller welcome

now you can (if you so desire) edit controllers\welcome_controller.rb

now create make index.rhtml (possibly move public.html there) in

and done!

Creating a new rails app

So…once you get it going how do you actually make rails work?

it’s not too bad

1) start a rails project.

rails projectName

2) Setup the DB to be what you want it to be (create migrations to setup the tables).

cd projectName
rake generate migration thisIsItsName

edit configure\db.yml to use sqlite3:

adapter: sqlite3
database: db/podCastBlender_development

adapter: sqlite3
database: db/podCastBlender_test

adapter: sqlite3
database: db/podCastBlender_production

3) run
ruby script/generate scaffold allthedatabasenames repeatthosenames
to setup some trivial database accessors/modifiers.

4) edit the models to reflect the ‘real’ relationships between the tables.
Say you have one named “users” and one named “objects” where each user has many objects?
The table for “objects” should be setup to have a field “user_id” (if user is the name of the owner)

then models\user.rb (each user has many objects, remember) is as follows:

class User < ActiveRecord::Base has_many :objects end

and objects.rb
class User < ActiveRecord::Base belongs_to :user end
[belongs_to means "I have a row that is the [primary] ID of this other entry in that table"

5) add code to use those relationships before saving the objects (like assign a user)--just a good idea generally 🙂

6) Do what you really wanted to do with the pages, etc.

How to install Ruby…

how to install ruby and get it runnin’ on windows with rails…with sqlite3
install ruby (should work) one click installer…

gem install rails --include-dependencies
gem install sqlite3-ruby (for windows, choose number 2:sqlite3-ruby 1.2.1 mswin32

now go to sqlite and download sqlite3 “dll without tcl bindings” and “executable” for sqlite3, copy sqlite3.exe, sqlite3.dll to ruby\bin directory
Then if the app is already developed:


rake migrate

now rock and roll.

If it is not ready then edit config\database.yml and change it to say something similar to:

adapter: sqlite3
database: db/podCastBlender_development

# Warning: The database defined as ‘test’ will be erased and
# re-generated from your development database when you run ‘rake’.
# Do not set this db to the same as development or production.

adapter: sqlite3
database: db/podCastBlender_test

adapter: sqlite3
database: db/podCastBlender_production
And you are done!