Difference between revisions of "Ruby on Rails"
From Tmplab
(→Gems) |
(→Debugging) |
||
(38 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | This is valid for Rails 2.2.2 up to 2.3.3, the versions i'm using currently. May be valid for other versions. | ||
+ | |||
= Fast commands = | = Fast commands = | ||
− | * Help yourself, use BORT for application baseline | + | * Help yourself, use [http://jimneath.org/2008/09/09/bort-base-rails-application/ BORT] for application baseline |
+ | |||
+ | == Schema == | ||
+ | ruby script/generate scaffold Project name:string description:text | ||
+ | ruby script/generate migration AddOwnerToProject | ||
+ | rake db:migrate | ||
+ | rake db:schema:dump | ||
+ | |||
+ | == Routes == | ||
+ | rake routes | ||
+ | |||
+ | = Running = | ||
+ | === Debugging === | ||
+ | # Install http://hoptoadapp.com/ or http://getexceptional.com/ | ||
+ | # Look in log/development.log | ||
+ | # Add "debugger" somewhere in the controller | ||
+ | # Relaunch the server using "ruby script/server --debugger" | ||
+ | ## now commands can be typed like "list", "up", "irb" (for an Interactive Ruby session) or "cont" | ||
+ | ## Remember the name of the top level object is "self" so printing something like "p self" can be useful | ||
+ | |||
+ | ==== rdebug / ruby-debug ==== | ||
+ | |||
+ | * http://pivotallabs.com/users/chad/blog/articles/366-ruby-debug-in-30-seconds-we-don-t-need-no-stinkin-gui- | ||
+ | |||
+ | gem install ruby-debug | ||
+ | |||
+ | ==== Debugging Rake tasks ==== | ||
+ | |||
+ | rdebug -n /opt/local/bin/rake db:migrate | ||
+ | rdebug -n /opt/local/bin/rake rake ENVVAR=VALUE db:migrate | ||
+ | |||
+ | with this magical line of code: | ||
+ | require 'ruby-debug'; debugger; | ||
+ | |||
+ | === script/server === | ||
+ | Usually with: | ||
+ | ruby script/server | ||
+ | |||
+ | === DB === | ||
+ | rake dbconsole | ||
+ | ruby script/dbconsole | ||
+ | |||
+ | === Ruby === | ||
+ | rake console | ||
+ | ruby script/console | ||
+ | then | ||
+ | irb | ||
= Troubleshooting = | = Troubleshooting = | ||
== Gems == | == Gems == | ||
− | + | gem install <gemname> | |
if it breaks, try to do: | if it breaks, try to do: | ||
Line 70: | Line 118: | ||
fixed the problem... well... you get the idea: there's a broken dependency in captistrano-ext for echoe.... | fixed the problem... well... you get the idea: there's a broken dependency in captistrano-ext for echoe.... | ||
+ | |||
+ | = Developping = | ||
+ | |||
+ | == CRUD == | ||
+ | [[Image:Rails-cycle.png]] | ||
+ | |||
+ | == Template applications == | ||
+ | * BORT | ||
+ | * "Also interesting is Blank, a starter Rails-app along the lines of Bort, but assuming use of shoulda rather than RSpec, among other things." http://philcrissman.com/2008/10/12/railsguts-blank http://jamesgolick.com/2008/10/10/blank-a-starter-app-for-r_c-and-shoulda-users | ||
+ | * "The sovereignity of Bort is a bit surprising given that it’s not the only player in the field by far - there are definitely others, like ThoughtBot’s suspenders, Blank by James Golick, starter-app by Pat Maddox, appstarter by Lattice Purple just to name a few." http://www.rubyrailways.com/rails-rumble-observations-part-ii-trends-in-gemplugin-usage/ | ||
+ | * http://www.railsinside.com/elsewhere/100-7-barebones-rails-apps-to-kick-start-your-development-process.html | ||
+ | * Suspenders http://robots.thoughtbot.com/post/159806262/suspenders | ||
+ | * Blank http://jamesgolick.com/2008/10/10/blank-a-starter-app-for-r_c-and-shoulda-users.html | ||
+ | * AppStarter (cannot find) | ||
+ | * Rails 2.3 applications template or boilerplate | ||
+ | ** http://m.onkey.org/2008/12/4/rails-templates | ||
+ | ** http://www.linux-mag.com/id/7302 | ||
+ | |||
+ | == BORT == | ||
+ | |||
+ | === Initialize a new BORT instance === | ||
+ | * Edit | ||
+ | <PRE> | ||
+ | ./config/database.yml | ||
+ | ./config/deploy.rb | ||
+ | ./config/environment.rb | ||
+ | ./config/environments/development.rb | ||
+ | ./config/environments/production.rb | ||
+ | ./config/environments/staging.rb | ||
+ | ./config/environments/test.rb | ||
+ | ./config/settings.yml | ||
+ | </PRE> | ||
+ | * Type | ||
+ | <PRE> | ||
+ | Rake db:migrate | ||
+ | </PRE> | ||
+ | |||
+ | === Modifying the Homepage === | ||
+ | Edit file: | ||
+ | <PRE> | ||
+ | app/views/sessions/new.html.erb | ||
+ | </PRE> | ||
+ | |||
+ | === Modifying the Database === | ||
+ | |||
+ | <PRE> | ||
+ | ruby script/generate migration AddProjectTable | ||
+ | </PRE> | ||
+ | |||
+ | or directly one of these: | ||
+ | <PRE> | ||
+ | ruby script/generate scaffold Project name:string description:text url:string | ||
+ | ruby script/generate scaffold Uploadbin hashid:string orig_filename:string stored_filename:string description:text url:string has_report:boolean owner_id:integer | ||
+ | </PRE> | ||
+ | |||
+ | === Deploy with Capistrano === | ||
+ | |||
+ | On the server host run: | ||
+ | <PRE> | ||
+ | ln -s myapp.mydomain.com penguins.dreamhost.com | ||
+ | mkdir -p ~/myapp.mydomain.com/releases | ||
+ | </PRE> | ||
+ | |||
+ | |||
+ | On local host, run | ||
+ | <PRE> | ||
+ | cap deploy:setup | ||
+ | cap deploy:migrations | ||
+ | </PRE> | ||
+ | |||
+ | === Deploying on Dreamhost === | ||
+ | |||
+ | <PRE> | ||
+ | ssh PROJECT@penguins.dreamhost.com mkdir -p git/PROJECT.git | ||
+ | ssh PROJECT@penguins.dreamhost.com "cd git/PROJECT.git/ && git --bare init" | ||
+ | git remote add origin ssh://PROJECT@penguins.dreamhost.com/~/git/PROJECT.git | ||
+ | git push origin master | ||
+ | ssh PROJECT@penguins.dreamhost.com "mv penguins.dreamhost.com old-penguins.dreamhost.com ; ln -s PROJECT.DOMAIN.com penguins.dreamhost.com" | ||
+ | ssh PROJECT@penguins.dreamhost.com "mkdir -p ~/PROJECT.DOMAIN.com/releases" | ||
+ | ssh PROJECT@penguins.dreamhost.com "touch ~/PROJECT.DOMAIN.com/index.php" | ||
+ | # GO TO dh panel to create MySQL db | ||
+ | cap deploy:setup | ||
+ | cap deploy:migrations | ||
+ | ssh PROJECT@penguins.dreamhost.com gem install term-ansicolor | ||
+ | ssh PROJECT@penguins.dreamhost.com gem install capistrano | ||
+ | ssh PROJECT@penguins.dreamhost.com 'echo "export PATH=\$PATH:~/.gem/ruby/1.8/bin" >> .bashrc; echo "export PATH=\$PATH:~/.gem/ruby/1.8/bin" >> .bash_profile' | ||
+ | ssh PROJECT@penguins.dreamhost.com 'cd `find PROJECT.DOMAIN.com/releases/|head -2|tail -1` && rake gems:install' | ||
+ | ssh PROJECT@penguins.dreamhost.com "ls -ald ./PROJECT.DOMAIN.com/current/public " | ||
+ | </PRE> |
Latest revision as of 00:48, 19 May 2010
This is valid for Rails 2.2.2 up to 2.3.3, the versions i'm using currently. May be valid for other versions.
Fast commands
- Help yourself, use BORT for application baseline
Schema
ruby script/generate scaffold Project name:string description:text ruby script/generate migration AddOwnerToProject rake db:migrate rake db:schema:dump
Routes
rake routes
Running
Debugging
- Install http://hoptoadapp.com/ or http://getexceptional.com/
- Look in log/development.log
- Add "debugger" somewhere in the controller
- Relaunch the server using "ruby script/server --debugger"
- now commands can be typed like "list", "up", "irb" (for an Interactive Ruby session) or "cont"
- Remember the name of the top level object is "self" so printing something like "p self" can be useful
rdebug / ruby-debug
gem install ruby-debug
Debugging Rake tasks
rdebug -n /opt/local/bin/rake db:migrate rdebug -n /opt/local/bin/rake rake ENVVAR=VALUE db:migrate
with this magical line of code:
require 'ruby-debug'; debugger;
script/server
Usually with:
ruby script/server
DB
rake dbconsole ruby script/dbconsole
Ruby
rake console ruby script/console
then
irb
Troubleshooting
Gems
gem install <gemname>
if it breaks, try to do:
gem install
if it still breaks, try to do:
irb require 'rubygems' gem 'myproblemgem', '1.0.0'
then look at the problem.
Example
for example this session shows that the "echoe" gem is not installed. When running script/server:
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require' script/server:3 Missing these required gems: capistrano-ext = 1.2.1 You're running: ruby 1.8.7.22 at /usr/local/bin/ruby rubygems 1.2.0 at /usr/local/lib/ruby/gems/1.8 Run `rake gems:install` to install the missing gems.
well... indeed Captistrano-ext IS installed:
$ ls -ald /usr/local/lib/ruby/gems/1.8/gems/cap* drwxr-xr-x 12 root wheel 408 Jan 20 12:11 /usr/local/lib/ruby/gems/1.8/gems/capistrano-2.5.3 drwxr-xr-x 9 root wheel 306 Jan 20 13:00 /usr/local/lib/ruby/gems/1.8/gems/capistrano-ext-1.2.1
but running the code with irb gives more details:
$ irb >> require 'rubygems' => false >> gem 'capistrano-ext', '1.2.1' Gem::LoadError: Could not find RubyGem echoe (>= 0) from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:578:in `report_activate_error' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:134:in `activate' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:158:in `activate' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `each' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `activate' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:158:in `activate' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `each' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `activate' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:158:in `activate' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `each' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `activate' from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:49:in `gem' from (irb):2
So a simple:
# gem install echoe Successfully installed echoe-3.0.2 1 gem installed Installing ri documentation for echoe-3.0.2... Installing RDoc documentation for echoe-3.0.2...
fixed the problem... well... you get the idea: there's a broken dependency in captistrano-ext for echoe....
Developping
CRUD
Template applications
- BORT
- "Also interesting is Blank, a starter Rails-app along the lines of Bort, but assuming use of shoulda rather than RSpec, among other things." http://philcrissman.com/2008/10/12/railsguts-blank http://jamesgolick.com/2008/10/10/blank-a-starter-app-for-r_c-and-shoulda-users
- "The sovereignity of Bort is a bit surprising given that it’s not the only player in the field by far - there are definitely others, like ThoughtBot’s suspenders, Blank by James Golick, starter-app by Pat Maddox, appstarter by Lattice Purple just to name a few." http://www.rubyrailways.com/rails-rumble-observations-part-ii-trends-in-gemplugin-usage/
- http://www.railsinside.com/elsewhere/100-7-barebones-rails-apps-to-kick-start-your-development-process.html
- Suspenders http://robots.thoughtbot.com/post/159806262/suspenders
- Blank http://jamesgolick.com/2008/10/10/blank-a-starter-app-for-r_c-and-shoulda-users.html
- AppStarter (cannot find)
- Rails 2.3 applications template or boilerplate
BORT
Initialize a new BORT instance
- Edit
./config/database.yml ./config/deploy.rb ./config/environment.rb ./config/environments/development.rb ./config/environments/production.rb ./config/environments/staging.rb ./config/environments/test.rb ./config/settings.yml
- Type
Rake db:migrate
Modifying the Homepage
Edit file:
app/views/sessions/new.html.erb
Modifying the Database
ruby script/generate migration AddProjectTable
or directly one of these:
ruby script/generate scaffold Project name:string description:text url:string ruby script/generate scaffold Uploadbin hashid:string orig_filename:string stored_filename:string description:text url:string has_report:boolean owner_id:integer
Deploy with Capistrano
On the server host run:
ln -s myapp.mydomain.com penguins.dreamhost.com mkdir -p ~/myapp.mydomain.com/releases
On local host, run
cap deploy:setup cap deploy:migrations
Deploying on Dreamhost
ssh PROJECT@penguins.dreamhost.com mkdir -p git/PROJECT.git ssh PROJECT@penguins.dreamhost.com "cd git/PROJECT.git/ && git --bare init" git remote add origin ssh://PROJECT@penguins.dreamhost.com/~/git/PROJECT.git git push origin master ssh PROJECT@penguins.dreamhost.com "mv penguins.dreamhost.com old-penguins.dreamhost.com ; ln -s PROJECT.DOMAIN.com penguins.dreamhost.com" ssh PROJECT@penguins.dreamhost.com "mkdir -p ~/PROJECT.DOMAIN.com/releases" ssh PROJECT@penguins.dreamhost.com "touch ~/PROJECT.DOMAIN.com/index.php" # GO TO dh panel to create MySQL db cap deploy:setup cap deploy:migrations ssh PROJECT@penguins.dreamhost.com gem install term-ansicolor ssh PROJECT@penguins.dreamhost.com gem install capistrano ssh PROJECT@penguins.dreamhost.com 'echo "export PATH=\$PATH:~/.gem/ruby/1.8/bin" >> .bashrc; echo "export PATH=\$PATH:~/.gem/ruby/1.8/bin" >> .bash_profile' ssh PROJECT@penguins.dreamhost.com 'cd `find PROJECT.DOMAIN.com/releases/|head -2|tail -1` && rake gems:install' ssh PROJECT@penguins.dreamhost.com "ls -ald ./PROJECT.DOMAIN.com/current/public "