Regenerate Model / View for new columns

27 views
Skip to first unread message

Balamurali Krishna

unread,
Aug 31, 2014, 1:19:21 PM8/31/14
to rubyonra...@googlegroups.com
I am trying to learn Ruby on Rails, so this may be a pretty basic
question.

I have added new columns to an existing table using
'rails generate migration add_column_to_table' command.

I don't see any change in model & view due to this.

So, I changed the all the views (like _form.html.erb & index.html.erb)
to include a new form element like:
<%= f.label :filename %><br>
<%= f.text_field :filename %>

where :filename was the new column.

When I run the rails, while the new column comes on UI, it doesn't get
saved into db. I think the binding between View and Model is missing.

Is there a way to add a new column in the table to all layers
(view/controller/model etc) apart from the db.

--
Posted via http://www.ruby-forum.com/.

Walter Lee Davis

unread,
Aug 31, 2014, 3:17:54 PM8/31/14
to rubyonra...@googlegroups.com
The scaffold generator only runs once, and it generates static files that you may edit yourself later. There was a time (in the pre -1.0 and 1.x era of Rails) when the scaffold was a live reflection of the database, and let you do things like you describe here -- change the database and the scaffold UI would change to reflect that. It hasn't worked that way for many years now.

If you open the Rails console, you will find that adding these columns to your database did create new methods in the model for you. If you added the filename to your database, then you would find that you could do YourModel.filename = "foo.bar" and that would work, and then you could read that back, and you could check if it had been set with filename? or save the model and get that value back later from the database. But you will need to add these fields to your form and your show and index page manually if you want to use them.

Like the dealer says, "first taste is free", and after that you have to break out your text editor and add some fields and outputs yourself.

Walter

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/080c37def7708ef100f8e76a87b9abc1%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Hassan Schroeder

unread,
Aug 31, 2014, 3:35:04 PM8/31/14
to rubyonrails-talk
On Sun, Aug 31, 2014 at 12:16 PM, Walter Lee Davis <wa...@wdstudio.com> wrote:

>> But you will need to add these fields to your form and your show and index page manually if you want to use them.

Also, in Rails 4.x:
http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Balamurali Krishna

unread,
Sep 1, 2014, 4:45:37 AM9/1/14
to rubyonra...@googlegroups.com
Thanks Hassan Schroeder & Walter Davis for the replies.
I had added the form element in view page.
But I had forgotten to add it to the "<object>_params" in the
controller.
So, the new element, although getting passed from UI wasn't getting into
the "create" methods. The issue is now fixed.

Thanks again.

Jason Fleetwood-Boldt

unread,
Sep 2, 2014, 12:37:06 PM9/2/14
to rubyonra...@googlegroups.com

I think what you might have missed is that rails generate only creates a migration file --- it doesn't actually put the instructions to do the migration into them migration file. It also doesn't' make any changes to your Model or View files (as you said).

look in db/migrate/ for a file that recently got created (it will have a timestamp like 20140902..... and part of its name will be "add_column_to_table")

If I'm right, when you open that file, you'll see empty up and down methods.

You'll want to add to the up method

add_column :users, :filename, :string


and to the down method

remove_column :users, :filename

(I just made up "users" since you didn't specify what model this is)

Once you do that, run rake db:migrate (be sure to do rake db:migrate RAILS_ENV=test for your test environment) and also stop & restart your webserver too!

-Jason
Reply all
Reply to author
Forward
0 new messages