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.

************************************************************** <h1>Listing artists</h1>

<table>   <tr>   </tr>

<% @artists.each do |artist| %>   <tr>     <td><%= link_to 'Show', artist %></td>     <td><%= link_to 'Edit', edit_artist_path(artist) %></td>     <td><%= link_to 'Destroy', artist, :confirm => 'Are you sure?', :method => :delete %></td>   </tr> <% end %> </table>

<br />

<%= 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!

Did you have a name column in your model?

-Dave

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

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

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

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.

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

>> 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 > *******************************************

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.

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.

anon_comp 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.

Scott Le gendre wrote:

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

[snip]

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!    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:

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?

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