Topic: Trying to reproduce the Jukebox Tutorial wint Rails 2.0.2....

rails jukebox -d mysql

>rails jukebox
>cd jukebox

##########
Edit your database.yml located in config/database.yml

Lets create the databases:

>rake db:create:all

This will create 3 databases:
jukebox_development
jukebox_test
jukebox_production

##########
>ruby script\generate scaffold artist name:string year:string type:string

>ruby script\generate scaffold albums title:string description:text

>ruby script\generate scaffold lyric song:string lyrics:text

Lets run the migration for all tables:

>rake db:migrate

== 1 CreateArtists: migrating =================================================
-- create_table(:artists)
   -> 0.1250s
== 1 CreateArtists: migrated (0.1250s) ========================================

== 2 CreateAlbums: migrating ==================================================
-- create_table(:albums)
   -> 0.1090s
== 2 CreateAlbums: migrated (0.1090s) =========================================

== 3 CreateLyrics: migrating ==================================================
-- create_table(:lyrics)
   -> 0.1090s
== 3 CreateLyrics: migrated (0.1090s) =========================================

>ruby script\server

##########

Everything looks good so far.

The problem start after setup the asociations:

In app/models/artist.rb

class Artist < ActiveRecord::Base
  has_many :albums
end

In app/models/album.rb

class Album < ActiveRecord::Base
  belongs_to :artist
end


In config/routes.rb

  map.resources :artists do |artist|
    artist.resources :albums
  end

##########

Now you can see all the routes:
> rake routes

Pretty cool command.

##########
But when trying to access. the nested resource for

http://127.0.0.1:3000/artists/1/albums/

Noticed and error on the page !!! need to comment for a second:

##########
<h1>Listing albums</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Year</th>
  </tr>

<% for album in @albums %>
  <tr>
    <td><%=h album.title %></td>
    <td><%=h album.year %></td>
    <td><%#= link_to 'Show', artist_album_path(album) %></td>
    <td><%#= link_to 'Edit', edit_artist_album_path(album) %></td>
    <td><%#= link_to 'Destroy', album, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%#= link_to 'New album', new_album_path %>

##########
I noticed some problem with the link_to.... so I fixed with some hard_code in the view...

    <td><%= link_to 'Show', artist_album_path(params[:artist_id] ,album) %></td>
    <td><%= link_to 'Edit', edit_artist_album_path(params[:artist_id] ,album) %></td>
    <td><%#= link_to 'Destroy', album, :confirm => 'Are you sure?',

<%= link_to 'New album', new_artist_album_path %>

##########
Later in another post on the Internet noticed:
http://www.akitaonrails.com/2007/12/12/ … l-tutorial

Which give me the idea to edit the albums_controller.rb:

before_filter :load_post
  def load_post
  @artist = Artist.find(params[:artist_id])
end

Then could use the properly

    <td><%= link_to 'Show', artist_album_path(@artist ,album) %></td>
    <td><%= link_to 'Edit', edit_artist_album_path(@artist ,album) %></td>
    <td><%#= link_to 'Destroy', album, :confirm => 'Are you sure?', :method => :delete %></td>

Due to the new routes the link_to New Album can be setup like:

<%= link_to 'New album', new_artist_album_path %>

I'm not sure how to call the Delete method. but I figure is a good start for a topic... Any suggestions are welcome !!

Best Regards Dinooz

Re: Trying to reproduce the Jukebox Tutorial wint Rails 2.0.2....

ActiveRecord::AssociationTypeMismatch in CommentsController#create

After try to follow the link: http://www.akitaonrails.com/2007/12/12/ … l-tutorial

Seems like still have the error:
ActiveRecord::AssociationTypeMismatch in CommentsController#create

Any advise will be appreciated....

Re: Trying to reproduce the Jukebox Tutorial wint Rails 2.0.2....

[ Same error for windows and Linux... ]
############
When click new comment this is the HTML Form....

<form action="/posts/1/comments" class="new_comment" id="new_comment" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="8bf68abea48b11899a13b6bed9c49d6d84412307" /></div>
  <p>
    <b>Post</b><br />
    <input id="comment_post" name="comment[post]" size="30" type="text" />
  </p>

  <p>
    <b>Body</b><br />

    <textarea cols="40" id="comment_body" name="comment[body]" rows="20"></textarea>
  </p>

  <p>
    <input id="comment_submit" name="commit" type="submit" value="Create" />
  </p>
</form>

<a href="/posts/1/comments">Back</a>


############
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:150:in `raise_on_type_mismatch'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/belongs_to_association.rb:22:in `replace'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1009:in `post='
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2117:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2117:in `attributes='
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2116:in `each'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2116:in `attributes='
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1926:in `initialize'
app/controllers/comments_controller.rb:47:in `new'
app/controllers/comments_controller.rb:47:in `create'

############
{"commit"=>"Create",
"post_id"=>"1",
"authenticity_token"=>"8bf68abea48b11899a13b6bed9c49d6d84412307",
"comment"=>{"body"=>"body I agree I agree !!!",
"post"=>"I agree on post 1"}}

############
I'm not really sure what is the problem... this happen when try to create a comment in a nested  route....

Re: Trying to reproduce the Jukebox Tutorial wint Rails 2.0.2....

I found this excelent link:

Nested Resources in Rails 2
http://adam.blog.heroku.com/past/2007/1 … n_rails_2/

I'm not sure at this time what would it be the real tutorial on a nested Resource even with 3 level's of nesting resources, is a little bit confusing right now... hard to figure it out where exactly is the problem. But maybe give you guys some ideas....

Agree with Adam: "It would be immensely convenient if there were a generator for this. Something like:"

generate nested_resource Ticket belongs_to:event

.... Perhaps some day... smile

Re: Trying to reproduce the Jukebox Tutorial wint Rails 2.0.2....

Just wanted to leave another link for another screencast: http://www.rubyplus.org/episodes/17-Rol … ls-2-.html

Looks good. still nothing on nested but the screencast looks good.

Happy New Year 2008 YALL !!!