Changes in a model not saving. Rails 4.1

五迷三道 提交于 2019-12-12 10:27:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!