问题
I've just created a user model using scaffolding, as follows:
rails g scaffold user name:string
I run db:migrate and everything works fine, but when I try to add a new field, it is not saving it in the database. I add the field by doing:
rails g migration add_surname_to_users surname:string
No weird messages when I run rake db:migrate, the database table is ok, the form has been edited to alow surname input, as it have the show and index views to display it properly. However, it is not saving the form value given into the database.
Any clues about how to fix it? It's quite annoying not being able to alter my models.
回答1:
If you're using Rails4
(I think so), make sure you have this new attribute into user_params
in your controller (because of strong_parameters
):
def user_params
params.require(:user).permit(:name, :surname)
end
It's a typical mistake when new to this Rails
version.
Further information here: strong_parameters
来源:https://stackoverflow.com/questions/23140679/changes-in-a-model-not-saving-rails-4-1