I'm just starting to learn ruby and rails, and I'm working through a
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
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
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.
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 %>
<b><%= column.human_name %>:</b> <%=h @license.send(column.name) %>
<% 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.
Coming from a similar background, I've gotten a lot out of:
and of course hanging out here and on the #ror irc channel