Ruby on Rails Sunday, August 31, 2014

On Aug 31, 2014, at 1:18 PM, Balamurali Krishna wrote:

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

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-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@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.

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/939342EE-A762-494F-8B78-B6FFE2534908%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment