Clint Laskowski Clint Laskowski -4 years ago 106
Ruby Question

Rails 5 add column to table and then update view

Perhaps a very n00b question. I asked Rails to create a simple scaffold:

> rails g scaffold Book title:string


Then I wanted to add the author:

> rails g migration AddAuthorToBooks author:string


Next I did a rails db:migrate and restarted my sever. So far, so good.

Finally I modified my form view and my index view by adding the author field (book.author).

I feel like I've done everything right when I enter an author into a new form or a edit an existing record and hit 'submit' I still can't see the author in the resulting HTML view. And, when I check the record in the Rails console it shows a value of nil.

Any ideas what I'm doing wrong? Or what I can check?

Answer Source

You must add the author column which you've added to the table now to the book_params method, something like this:

def book_params
  params.require(:book).permit(:title, :author)
end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download