symbols disallowed in windows filenames

So…say you want to write a Ruby function to “sanitize” file names so that users can’t input file names and overwrite other files, or save theirs in odd places, etc?

Turns out that

Windows disallows:

? * / \ | < >

And the following are sensitive in Linux:
! & ; : ” ‘ ~ `
(at least for commands run in bash) so…

sanitizedName = @user_blended_cast.name.gsub(/[`\.?*\/\\|<>!&;:"'~@#\$%\^\(\)]/, '_')

Which replaces those bad boys with an underscore. So that’s my attempt at a file sanitize function so you can use it on the command line, etc.

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
config/routes.rb
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:

….app/controllers/whatever.rb
def index
list
render :action => ‘list’
end

So if you so desire and do not have a “welcome” class
then
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
app\views\welcome

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:

development:
adapter: sqlite3
database: db/podCastBlender_development

test:
adapter: sqlite3
database: db/podCastBlender_test

production:
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.

Roger's meanderings, notes to himself, bug reports, and other things