db change not reflected in app

Hi folks.

I'm just starting to learn ruby and rails, and I'm working through a tutorial.

I've hit a hurdle attempting to add a column to a table. I've created the migration file and ran it through rake, and I can see that the db (mysql) has changed.

However, from what I've read, this new column should immediately be reflected in the app; in other words, when I create a new widget in the app, a new form field should appear on the page. But, this isn't happening.

Can anybody explain why? Thanks!

Being "reflected in the app" and appearing on the page are two different things.

The field *is* available as a method on the model, but it's not going to appear on any page (other than a list that is iterating through all the model's columns) unless you put it there. It would be highly annoying if every time you added a field it appeared randomly on views :slight_smile:

You need to edit the view file (the erb, or rhtml if it's an older version of Rails) and put the HTML to display the field you've created wherever you want it to appear.

if you want to be *sure*, you can add <%= @model.my_new_field %> anywhere in the view, and edit a record in the DB - refresh to see your DB-edited value.

Michael Pavling wrote:

Which version of the book have you got?! If it's v2 or earlier, lots of it is going to be wrong for the version you have installed, which is going to be very frustrating at times.

But yes - the scaffolding used to loop through all the columns :

<% for column in License.content_columns %> <p>   <b><%= column.human_name %>:</b> <%=h @license.send(column.name) %> </p> <% end %>

...but that's intensely annoying because you have to *totally* remove it to replace it with anything useful.

Now, the individual fields get rendered one after an other - so you can tweak the scaffolded file, rather than re-write it completely.

(of course the down side is if you add a new field to the db, you have to add it to the view)

You're setting yourself up for continuous pain and frustration by using an out-of-date reference with a current Rails version.

Toss that book and get the latest, or use online tutorials based on the 2.3.x branch. Really.

Michael Pavling wrote:

According to "Agile Web Development with Rails", the field should automagically appear on views, but that was using rails 1.2 and using scaffold in the controller. I'm running rails 2.3.x, so wasn't able to use "scaffold" in the way described in the book.

Does that explain the (mis)behavior?

Which version of the book have you got?! If it's v2 or earlier, lots of it is going to be wrong for the version you have installed, which is going to be very frustrating at times.

2nd edition, 2006.

But yes - the scaffolding used to loop through all the columns :

<% for column in License.content_columns %> <p>   <b><%= column.human_name %>:</b> <%=h @license.send(column.name) %> </p> <% end %>

...but that's intensely annoying because you have to *totally* remove it to replace it with anything useful.

Now, the individual fields get rendered one after an other - so you can tweak the scaffolded file, rather than re-write it completely.

(of course the down side is if you add a new field to the db, you have to add it to the view)

Thank you.

Hassan Schroeder wrote:

Coming from a similar background, I've gotten a lot out of:

  http://guides.rubyonrails.org/   http://railscasts.com/   http://peepcode.com/

and of course hanging out here and on the #ror irc channel :slight_smile:

HTH,