Ruby on Rails

Gems Freezing Remember that when you freeze something, it goes in your vendor directory. This is how I was able to run rails on my machine w/o installing the rails gem. However, when I went to create a new project, “rails” wasn't installed, so my machine didn't know WTF rails was. To fix this: I ran

sudo gem install -y rails

FIXME Find out what the hell the -y does

Models

 script/generate Model Recipe
  • Creates model, unit test, migration and fixtures.

Controllers

script/generate scaffold Recipe

Console

General

app/controllers/application.rb is the parent of all controllers. If you want a general method for all controllers, put it there.

Forms / Rhtml

Checkboxes

Gems

If gems or script/generate whines with this error:

undefined method 'gem' for main:Object
from 1.8/rubygems/custom_require.rb

Then try running

gem update --system

See Also

Freezing

Testing

Login Stuff

Excellent tutorial on Roll-your-own http://www.aidanf.net/rails/rails_user_authentication_tutorial

Warnings, and the filtering of the log TODO were eliminated by the tar file found here: http://www.chrissterritt.com/AidanLoginRails2.tar

Filters

Good explanation for :before_filter problem with application.rb and friends.

http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html

Handling The Request

Request data is available in the params hash. The data dump of the request can be usually be seen in the /log/development.log file.

Ruby/Rails data dumps look something like this:

Parameters: {"game"=>{"event_id"=>"3", "description"=>"A one man game", "players"=>["3", "1"]}}

The rules to remember are:

  • Square brackets [] denotes an array.
  • Curly braces denotes a hash.

The following examples show how to retrieve crap from the request:

Code Data “Type” Returns the Value
params[:result][:game][:event_id] Hash Value 3
params[:result][:game][:description] Hash Value “A one man game”
params[:result][:game][:players][0] Array Value (Note the “players”⇒[“3”, “1”] above) “3”

FIXME ruby Show kewl ruby data types / methods used to step through data types / collections.

Generating Models, Scaffolding, Controllers

  • Don’t need to generate scaffolding / models on existing models when you just add a model
  • If you run generate model (see below), it will create the migration step in db/migrate. You can then edit the migration script and run rake:migrate, so you don't have to dork with MySQL stuff when creating a table.
script/generate model Hand

Now, edit the newly created xxx_create_hands.rb file to include the fields, etc.

Migrations

Helpful Links

Rails Version

To detect the version of rails you're running perform the following steps

Use Script/Console

Browser

You can see all the versions of the major components of rails by going to http://localhost:3000/rails/info/properties

Make sure you start your rails application before trying it, though :)

> script/server

script/console

You can also use the script/console program.

Go to your rails application's base directory.

> script/console

At the » prompt, type the following:

>> Rails::VERSION::STRING

You should see some output like:

=> "1.2.1"

Site5 Specific

http://forums.site5.com/showthread.php?t=20695&highlight=dispatch.fcgi

  • Create rails app (preferably in ~/apps folder)
rails <myapp>
  • Create a subdomain if necessary.
  • Create a symlink in the public_html folder that points to your rails app's public folder.
ln -s ~/<railsapp>/public ~/public_html/<somequickname>
  • Edit ~/<railsapp>/public/.htaccess, and change
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

to

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
  • Edit ~/<railsapp>/config/database.yml and put in database's name, username/password that you use to log into ssh.
  • Edit ~/<railsapp>/config/environment.rb and change Note I'm not sure about this one.
#ENV['RAILS_ENV'] ||= 'production'

to

ENV['RAILS_ENV'] = 'development'
Also, follow this link http://forums.site5.com/showthread.php?t=11954 for more junk on rails on site5.  I also have this link bookmarked / cached.

Dispatch.fcgi

The Dispatch.fcgi that comes with rails XXX has an incorrect bang! operator for Site5.

Old

#!/opt/local/bin/ruby

New

#!/opt/local/bin/ruby

Restart Rails / Web Server

I've recently found that just issuing the touch -m command on public/dispatch.fcgi will do a server restart. This is much better than the previous way of having to search for ruby dispatch.fcgi that belong to You, and killing the pids.

From the root of your rails app:

touch -m public/disptach.fcgi

Logging

See

Routing Requests

See chapter 16.3 in AWDWR

map.connect '', :controller => "time_entries"

Login / Logout

General Tips

Make sure that “From” e-mail address is set up in

config/environments/user_environment.rb

FIXME Rails

* Rails will choose the layout in the app/views/layouts directory that has the same name of the controller that's handling a request that has the same name of the controller

content_for_layout variable is a “magic” one, whose content is rendered by Rails.

So if you put <%= @content_for_layout %> somewhere, then the layout will receive all the doodoo that's in the xxx.rhtml page that's using the layout.

ERb = “Embedded Ruby”

link_to_unless_current = a function in Rails that creates a link to a page, unless it's the current page! Yippee!

The link_to function uses routes.rb to figure out where to create the link to. For example,you can put in link_to :action⇒index,and rails figures out which index you're talking ab00t

Stylesheets are located in public/<controller>.css, methinks. Pge 40 RailsSpace

Start using styles immediately for your aep, and thinking about presentations and layouts, etc.

C» usr.save ⇒ false

usr.errors.on(:screen_name)

⇒ “is too short (minimum is 4 characters)”

usr.errors.on(:password)

⇒ “is too short (minimum is 4 characters)”

Ctrl-N is the auto-complete crap for vim. use reload! to reload the environment in irb

TODO: get new \1, \2, etc from mappings.vim FIXME

Capistrano

When All Else Fails

Check the error logs on Site5's backdoor application. If it's whining about some permissions, change the permissions to the public folder in the app/current/public folder to 755 BEOTCH! TODO: Check the URLs above for hints about how to chmod 755 pubic erectory fodler.

Bugs / Fixes

ERRNO:ENOENT

Error when trying to run script/console on Edgy.

http://www.ruby-forum.com/topic/80494

solution:

sudo ln -s /usr/bin/irb1.8 /usr/bin/irb

/tmp/mysql/sock

Problem : When first setting up a database on Ubuntu Edgy, you get No such file or directory - /tmp/mysql.sock Solution: Specify socket: /path/to/socket.sock in config/database.yml for the database you're running on Ubuntu. (Note, make sure you don't have the socket: token on production, if it's not needed.)

production:

adapter: mysql
socket : /var/run/mysqld/mysqld.sock
...

See Also

Craig's Opinion on Rails Projects

Rails Generator

PageDescriptionTags
Action Mailer The Rails Action Mailer sends e-mail using an MVC based approach. You call deliver_<something> where <something> is a method in your controller. You provide … , , ,
Autotest sudo gem install ZenTest <http://notesmine.com/rails_application_documentation?s=rdoc> When you run autotest, you see the results zoom by on the terminal. What… , , ,
Capistrano <http://wiki.dreamhost.com/index.php/Capistrano> View Avail. Tasks See what tasks are available-—including both your own custom tasks and the standard ones:… , , , ,
Emacs Rails I will give Emacs one more try. Vim is nice, but the windows/buffers are so annoying that I want to try Emacs. I followed the instructions on this page up to … , , ,
GemRunner You might get this error on Ubuntu, when trying to run rails after upgrading RubyGems /usr/bin/gem:23: uninitialized constant Gem::GemRunner (NameError) See <… , ,
Grails Versus Rails Get the flamethrowers out! When updating constraints, I've had to restart the Grails server. I don't seem to recall having to restart the server as much when … ,
RSpec Gotchas On one of my Rails projects, I had environment.rb explicitly set ENV['RAILS_ENV'] to 'development'. In older versions of RSpec, RSpec sets the ENV['RAILS_ENV']… , ,
Rails Automagic This page is devoted to some of the obscure magical stuff that Rails does. Somehow, rails wraps the form fields that have errors with a <div> tag that has a cl… , , ,
Rails Documentation A decent place to get documentation for rails is in your own project directory. From the root of your project, run rake doc:rails The files will be created i… ,
Rails ERb ERB stands for “Embedded Ruby” If you put a minus sign before the end tag (-%>), then no newline is rendered. rails, erb, rhtml, views , , ,
Rails Environment You can find out the versions of much of your Rails application by starting the server script/server Then go to this page: <http://localhost:3000/rails/info/p… ,
Rails Example After working with rails for the past three months, I often find myself wasting enormous amounts of time trying to do relatively simple things in my projects. … , , ,
Rails Forms See railscasts.com episode 16 about virtual attributes, which can be used to make it easier to modify two models from within the same form w/o bloating the cont…
Rails Group <http://stlrails.org/> <http://groups.google.com/group/stlrails?hl=en> rails, groups , ,
Rails Highlights Bragging stuff about rails. Kind of an antithesis to rails whines. Find Or Initialize Many times, you want to create an object if none exists. Otherwise, yo… ,
Rails Project Documentation There's more than a few ways to provide documentation for your project. /doc/README_FOR_APP is a text file in all rails projects where you can put helpful poin… ,
Rails Server The Rails Way book has a chapter on setting up a Rails production server. This page is pretty much a ubuntu-specialized version of that chapter. I found anoth… , , , , ,
Rails Sessions Rails provides a method called ActiveRecordStore to store sessions in the database, rather than on disk, which is the default. rake db:sessions:create In conf… , ,
Rails Whines If I create a migration, and another person creates a migration, then you could end up with the following situation: User 1 001_create_foo.rb User 2: 001_creat… ,
Rake Ruby Make * Uses Ruby (not stupid XML like Ant does). Get a list of all rake tasks available. rake -T or rake --tasks Show only tasks containing spec rak… ,
Rspec RSpec is an alternative to the TestUnit tool which is built into Rails. RSpec users claim these advantages over Test::Unit * Easy to read * Built for BD… , , , , , ,
SQL Lite A lightweight database that's good for development/test because it's fast. As far as I can tell, Xubuntu and Ubuntu are shipped with the junk that rails needs… , ,
Testing in Rails Testing is a huge component of Rails, and my notes won't fit on one page. * Units - Basic tests of Models * Functional - Testing of views/controllers * I… , , ,
URL Helper I put this note here to help myself gain an understanding of rails and how everything fits together. <http://api.rubyonrails.com/classes/ActionView/Helpers/Url… , ,
Vim Rails Great vim plugin for rails applications. There's an excellent Tutorial on , and I recommend you navigate through that to learn how to install and use. I liste… , , , , , , , , ,
Vim Rails Screencast I might create a screencast showing how to use Vim w/Rails. * Show Vim setup? -- No * Show where to get Rails & related plugins? - Yes * Create Rails ap… , ,

TODO

 
rails.txt · Last modified: 2008/03/06 17_55 by tookelso
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki