Skip to content

Bootcamp May 16 More Git; Nested Resources; Nested Layouts

ahmetaktay edited this page Sep 13, 2010 · 6 revisions

[ if you can see this, anyone can edit our training pages. not that anyone would want to… anyways, a good morning to everyone from Istanbul. May all your rakes succeed and good luck on achieving ultimate Harleyness.]

Other useful Git commands

1. Temporarily saves changes that you don’t want to commit immediately for later. Can re-apply the saved changes at any time.

git stash

Search through your git trees of content for words and phrases without having to actually check them out.

2. Let’s say if I want to check if the current branch has the mass add code: (note -n is to display line number next to the filename)

git grep -n mass_add

Nested resources

So we have *users and *comments — logically *comments resources are children of *users resources. Remember that a user has_many comments and a comment belongs_to user? Therefore we use this:


map.resources :users do |user|
  user.resources :comments
end

Or in a shorter form:

map.resources :users, :has_many => :comments

Rule of thumb to prevent abuse: should not have more than 1 level of nested-ness

Now your app can recognize url like


GET    /users/1/comments         ---> index
POST   /users/1/comments         ---> create
GET    /users/1/comments/new     ---> new
GET    /users/1/comments/1/edit  ---> edit
GET    /users/1/comments/1       ---> show
GET    /users/1/comments/1       ---> update
DELETE /users/1/comments/1       ---> delete
 

It seems nice to have /users/1/comments/new right? Think that /comments/1 is enough? True, each comment already identifies which user anyway. If you want this, add :shallow => true, such as:


map.resources :users, :shallow => true do |user| 
  user.resources :comments
end

#or
map.resources :users, :has_many => comments, :shallow => true %

Usage:


<%= link_to "Add comment", new_user_comment_path(@user) %>
<%= link_to "Show comment", user_comment_path(@user, @comment) %>
# or simply:
<%= link_to "Show comment", [@user, @comment] %>

The [@user,@comment] is quite cool because it can be quite generic [a, @b] and it takes you to the routes <code>_ca_cb_path(a,@b) where ca and cb are model names of @a and @b. This can be useful when your form allows people select different types of objects and you should provide a link to view them and think automatically takes you to the show action.

I’ll also review adding manual actions to nested routes

Okay anyway there are always better guides out there. One good place to look is the awesome railsguide. For details on routing, go here:http://guides.rubyonrails.org/routing.html

Nested layouts

First understand what *yield is and does
Next try taking advantage of yield to place page title, html head, and other stuff in the right place in the layout. Hint: use yield(:title) and yield(:head) and use content_for :something block to define values for that yield(:something)

There are a few ways to deal with nested layouts. Googling shows some ‘alright’ method but not that great. We’ll discuss some ways to do it and see what everybody prefers.

Again you can read more about layouts and rendering in Rails in general from the awesome railsguide here:http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts