Scaffolding working...kinda

Hello all. I’ve been working through some online tutorials, learning ROR
and have a question on scaffolding. Through the command promt i’ve ran
the command:

$ script/generate scaffold Albums

This created the controllers, helpers, models, and views.
I have a database table titled “albums” and have two records saved
within

The problem is that the scaffold command did not create what I had
expected it to create within my views. It only created partial code.
Below is the code it generated. The same goes for all my Albums views.


Listing artists

<% @artists.each do |artist| %>

<% end %>
<%= link_to 'Show', artist %> <%= link_to 'Edit', edit_artist_path(artist) %> <%= link_to 'Destroy', artist, :confirm => 'Are you sure?', :method => :delete %>

<%= link_to ‘New artist’, new_artist_path %>


When I view the page, it displays the “Show” “Edit” “Destroy” links, and
displays these for the number of items I have within the database. I
added an additional item to the database and the “Show” “Edit” “Destroy”
was displayed an additional time, making the total three to match the
number of records I have in the database table.

So i went one step further, and figured out that i’m missing the Table
Headings along with the elements to represent what data is supposed to
be displaed. For example, if I type

<%=h album.id %>. Now the
id is displayed for each of the three records within the Albums table.

So, the question is, why isnt it filling out the code I had expected? I
have over come a lot of issues, mainly because of the tutorial i am
walking through is using an older version of ROR. This issue I cant find
an answer on. Anyone have any ideas that could point me in the right
direction?

Thanks!


Listing artists

<% @artists.each do |artist| %>

<% end %>
<%= link_to 'Show', artist %> <%= link_to 'Edit', edit_artist_path(artist) %> <%= link_to 'Destroy', artist, :confirm => 'Are you sure?', :method => :delete %>

<%= link_to ‘New artist’, new_artist_path %>


Sorry, I pasted in the incorrect view. You get the idea. Just replace
“artists” with albums. The scaffolding issue has to do with both albums
and artists. Hope I didnt make it more confusing.

Thanks!

On Thu, Aug 12, 2010 at 13:56, Scott Le gendre [email protected]
wrote:

The problem is that the scaffold command did not create what I had
expected it to create within my views. It only created partial code.

Did you have a name column in your model?

-Dave


Specialization is for insects. -RAH | Have Pun, Will Babble! -me
Programming Blog: http://codosaur.us | Work: http://davearonson.com
Leadership Blog: http://dare2xl.com | Play: http://davearonson.net

Dave A. wrote:
Did you have a name column in your model?

Well, all I have in my model is the following:


class Artist < ActiveRecord::Base

has_many :albums

end


On Thu, Aug 12, 2010 at 15:31, Scott Le gendre [email protected]
wrote:

end


That may be all you’ve specified in the model file, but have you any
additional columns in the database? If you had some, then when you
generated the scaffolding, I think it would have put them in the view.
Try a new project, similar in most respects, but make some data
columns, including some kind of name/title/whatever, before you
generate the scaffold. Then compare the two.

-Dave


Specialization is for insects. -RAH | Have Pun, Will Babble! -me
Programming Blog: http://codosaur.us | Work: http://davearonson.com
Leadership Blog: http://dare2xl.com | Play: http://davearonson.net

On 12 August 2010 18:56, Scott Le gendre [email protected] wrote:

The problem is that the scaffold command did not create what I had
expected it to create within my views. It only created partial code.
Below is the code it generated. The same goes for all my Albums views.

The scaffold does not look at an existing table. You can tell it what
the columns you want are, and it will create a migration to create the
table for you, and will include the columns in the view. The Getting
Started rails guide at http://guides.rubyonrails.org/ explains how it
works. I recommend all that all learners work through that guide and
the others there.

Colin

anon_comp wrote:

On Aug 12, 3:36�pm, Dave A. [email protected]
wrote:

�has_many :albums

Regenerate the scaffold. I believe you’re following an old tutorial,
scaffolds are a lot simpler than trying to attach tables together and
hoping they’ll stick. Take note of Colin’s comment.

$ script/generate scaffold album title:string artist:string
price:decimal

then take a look in /db/migrate/StringOfNumbers_create_albums.rb

Finally, try running it.

Got it, thanks guys. The problem was I need to use this type syntax:

“script/generate scaffold Album title:string artist:string
date_time:datetime”

Within the video tutorial, only the syntax “script/generate scaffold
Album” was used and it created all the correct views. So not sure whats
going on with that. There is an option that may reproduce what I see in
the video tutorial. Its called “CRUD scaffold generator” and can be
found here: http://github.com/kevinskoglund/crud-scaffold-generator

Thanks for everyones help.

On Aug 12, 3:36 pm, Dave A. [email protected]
wrote:

has_many :albums

Regenerate the scaffold. I believe you’re following an old tutorial,
scaffolds are a lot simpler than trying to attach tables together and
hoping they’ll stick. Take note of Colin’s comment.

$ script/generate scaffold album title:string artist:string
price:decimal

then take a look in /db/migrate/StringOfNumbers_create_albums.rb

Finally, try running it.

Scott Le gendre wrote:

walking through is using an older version of ROR. This issue I cant find
an answer on. Anyone have any ideas that could point me in the right
direction?

Thanks!

The main thing to remember is that scaffold is just a tool used to build
your app, it wont build it for you. It is intended to be temporary, just
like a scaffold in other building trades. You use it to get a starting
framework, and then customize it to get your final product.

As Colin pointed out, you can give it some arguments to help it tell
which fields to create, and then it will include those fields in all of
the files it produces, but with out those hints, it has no way of
knowing what you wanted it to create.

So use it to get started, but then be prepared to do the work your self.

– Will

So use it to get started, but then be prepared to do the work your self.

Will, thanks for the comments and fully noted! I’m doing a tutorial
through lynda.com titled “Ruby on Rails Essential Training”. The
tutorial is really great, but severely outdated. The tutorial goes
through building all the things scaffold automates first, then after
you’ve completed all the work, he shows you scaffold. Now, he is going
through the code scaffold creates and customizing it. Really great
besides the out of date syntax.

Have any suggestions on ore up to date ROR tutorials I could dig in?

Scott Le gendre wrote:

Have any suggestions on ore up to date ROR tutorials I could dig in?

One thing I think I remember is that in Rails < 2.0 (that is, 1.x, or
many years old) the scaffold command would examine any table that
existed in the database and build a scaffold that used the fields it
found there. In 2.0 they changed this behavior. If your tutorial dates
from the 1.x days, many of the claims it may make about what the
scaffold command will do are now wrong.

Since 2.0 (I think, certainly by now) the scaffold command assumes that
there is no table in the database, and generates a migration to create
the table. But you have to tell it what fields to create.

As for how to learn it, the best way is to just use it. Read the
documentation, try stuff and see what it does. When you get a little
further, look at the generator code to really understand what it does.

Good Luck,

– Will