Doug Bryant

Tech thoughts and notes

OSCON 2005 - Day 1 (Rails W/ DHH)

Kickoff – should be a code heavy presentation…

Install ruby dated 12/25/2004

gem install rails to install rails (requires ruby gems)

Demo based around creating a blog app.

David is giving a detailed explanation of directory structure and built in webserver. Seems to be standard stuff in docs. Major point is that everything is layed out for you so you can get started more quickly and do not have to make those decisions.

… Speaks about the ./script/generate program which stubs out controllers, models, etc.

if a rhtml file is named the same as a method on the appropriate controller, that rhtml file will be rendered by default as the return from the action method.

precoderedirect_to :action = “method_name”/code/pre will jump to another action method on controller.

./script/destroy can remove models/controller/etc just like it can generate them.

Rails has extended the to_s method to include parameters (for date) so you can say my_date.to_s(:long) or my_date.to_s(:short)

foreign key names are not plural – should be same as model name. fk or comments to posts is post_id not posts_id (sorry if that one is not in context of anything else)

can specify foreign keys when defining the relationship
precodehas_many :comments, :foreign_key = “crazy_dba_convention_id”/code/pre

with ./script/console you can interact directly with your model.

Pass in :locals { :variable = my_variable } you can render specific to what ever scope you are in.

Foreign key collections have extra convience methods such as create and build.
@post.comments.create(blah) where comments is a collections of comments on post.

layout the reverse of jsp include. view maps to controller name and is used as a template for any html that needs to be rendered for that controller.

Use ApplicationController which was generated as part of generating your project for extension points such as authentication (ApplicationController is a base class of all other generated Action Controllers)

Unit tests with fixtures, fixtures load the entire fixture file for that db class and loads the values into instance variables by their name.

precode
def test_fixtures_work
assert_equal “FooBarBaz”, @my_first_post[‘title’]
end
/code/pre
Where there is a fixture entry with the title my_first_post with the title of ‘FooBarBaz’

Unit tests usually only useful for testing your methods and the domain model.

Functional Testing Mocks already built for simulating gets and posts. Used for testing controllers and workflow of controllers. Can assert things such as responses (assert_response :success assert_template ‘foobar’)

When functional testing, you can test create or update methods similar to this: (code may make no sense, but methods will be like this)
precode
def test_create_post
post :create, :post = { :title = “foo”, :body = “bar” }
assert_response :redirect
assert_kind_of Post, Post.find_by_title(“This is my title”)
post :create, :post = { :title = "", :body = “bar” }
assert_response :success
assert_equal “don’t leave me out”, assigns(:post) .errors_on(:title)
end
/code/pre

AJAX
In template do
precode
%= javascript_include :defaults %
/code/pre
damnit, think the javascript_include is correct but may be wrong – DHH moved on.

use the remote call replacement (linkto_remote instead of link_to for example) and pass in parameters (controller, id of element in html, etc)

can call precoderender :nothing = true/code/pre in action if nothing needs to be rendered for ajax call

can distinguish ajxx from non ajax by using the method request.xml_http_request?

DHH must be tired – he’s ajaxing the wrong files and it’s getting slightly confusing.

Need to clear input fields when ajaxing a form. Example if you have inputs for name and address, you will have to clear the name and address input boxes.