Created
January 17, 2010 00:56
-
-
Save coderberry/279112 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.- rails jukebox -d mysql | |
2.- cd jukebox | |
3.- vi config/database.yml | |
4.- rake db:create:all | |
5.- ruby script/generate scaffold Artist name:string permalink:string albums_count:integer | |
6.- ruby script/generate scaffold Album artist:references title:string permalink:string | |
7.- Edit your db/migrate 001_create_artists migration file to initialize the counter cache for artists_count and add the index for the permalink column. | |
def self.up | |
create_table :artists do |t| | |
t.string :name | |
t.string :permalink | |
t.integer :albums_count, :default => 0 | |
t.timestamps | |
end | |
add_index :artists, :permalink | |
end | |
8.- Similar process for the 002_create_albums migration file, we need to add the index for the permalink column. | |
def self.up | |
create_table :albums do |t| | |
t.references :artist | |
t.string :title | |
t.string :permalink | |
t.timestamps | |
end | |
add_index :albums, :permalink | |
end | |
9.- rake db:migrate | |
10.- delete the public/index.html and update your config/routes.rb file with the default route to the artists controller: | |
map.root :controller => 'artists' | |
11.- Update the Model files for our asociations and allow counter_cache. | |
Edit the app/models/artist.rb | |
has_many :albums | |
Edit the app/models/album.rb | |
belongs_to :artist, :counter_cache => true | |
12.- Edit your Artists views and delete or comment part of the form to insert or edit Artists count since is a counter cache, | |
we don't need to update manually | |
From the app/views/artists/new.html.erb and app/views/artists/edit.html.erb remove the Artists_count field... | |
<!-- | |
<p> | |
<%= f.label :albums_count %><br /> | |
<%= f.text_field :albums_count %> | |
</p> | |
<p> | |
--> | |
13.- Lets prepare to for Permalink: | |
Add to your Artist model file app/model/artist.rb: | |
class Artist < ActiveRecord::Base | |
has_many :albums | |
def to_param | |
permalink | |
end | |
end | |
*** REPEAT :: Add to_param function for the app/model/album.rb: | |
class Album < ActiveRecord::Base | |
belongs_to :artist, :counter_cache => true | |
def to_param | |
permalink | |
end | |
end | |
14.- To make this work lets update the Artist controller file app/controller/artists_controller.rb | |
Replace: Artist.find with Artist.find_by_permalink everywhere except the index action. | |
*** Repeat to the Albums controller app/controller/albums_controller.rb: | |
Replace: Album.find with Album.find_by_permalink everywhere except the index action. | |
15.- Edit the New and Edit file for the Albums model: app/views/albums/new.html.erb | |
<b>Artist</b><br /> | |
<%#= f.text_field :artist %> | |
<%= collection_select(:album, :artist_id, Artist.find(:all), :id, :name, {:prompt => true}) %> | |
</p> | |
Now for the second file app/views/albums/edit.html.erb: | |
<b>Artist</b><br /> | |
<%#= f.text_field :artist %> | |
<%= collection_select(:album, :artist_id, Artist.find(:all), :id, :name, {:prompt => false}) %> | |
</p> | |
This change will retrieve the list of the current selected Artists in the select box. | |
16.- The last step some required validations for the appropiate operation of the application: | |
For the Artists: app/models/artist.rb | |
validates_presence_of :name | |
validates_presence_of :permalink | |
validates_uniqueness_of :name | |
validates_uniqueness_of :permalink | |
attr_accessible :name, :permalink | |
For the Albums: app/models/album.rb: | |
validates_presence_of :artist | |
validates_presence_of :title | |
validates_presence_of :permalink | |
validates_uniqueness_of :title | |
validates_uniqueness_of :permalink | |
17.- app/controllers/albums_controller.rb Sort by Artist.... | |
#@albums = Album.all | |
@albums = Album.find(:all, :order => "Artist_id DESC") | |
app/controllers/artists_controller.rb Sort by Name.... | |
#@artists = Artist.all | |
@artists = Artist.find(:all, :order => "Name ASC") | |
18.- Change the index/view to show properly... | |
app/views/albums/index.html.erb | |
<td><%=h album.artist %></td> | |
<td><%=h album.artist.name %></td> | |
19.- Quick Navigation | |
<%= link_to 'Artists', artists_path %> | | |
<%= link_to 'Albums', albums_path %> | |
20.- Add the Detail for Artist Albums... | |
List the albums for a particular artist: | |
app/views/artists/show.html.erb | |
<hr width="60%" size="1"> | |
<table> | |
<tr> | |
<th>Artist</th> | |
<th>Title</th> | |
<th>Permalink</th> | |
</tr> | |
<% @artist.albums.each do |album| %> | |
<tr> | |
<td><%=h album.artist.name %></td> | |
<td><%=h album.title %></td> | |
<td><%=h album.permalink %></td> | |
<td><%= link_to 'Show', album %></td> | |
<td><%= link_to 'Edit', edit_album_path(album) %></td> | |
<td><%= link_to 'Destroy', album, :confirm => 'Are you sure?', :method => :delete %></td> | |
</tr> | |
<% end %> | |
</table> | |
<hr width="60%" size="1"> | |
21 BlackJack with Eric Berry Themes Plugin !!! | |
http://github.com/cavneb/themes | |
./script/plugin install git://github.com/cavneb/themes.git | |
vi app/controllers/application_controller.rb | |
layout 'themes' | |
:: Delete default controller application. | |
That's All folks go a head and start the web server... | |
ruby script\server | |
Feel free to navigate in your application at: | |
http://localhost:3000/artists | |
http://localhost:3000/albums |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment