模型中的更改未保存.导轨 4.1 [英] Changes in a model not saving. Rails 4.1

查看:28
本文介绍了模型中的更改未保存.导轨 4.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用脚手架创建了一个用户模型,如下所示:

I've just created a user model using scaffolding, as follows:

rails g scaffold user name:string

我运行 db:migrate 并且一切正常,但是当我尝试添加新字段时,它没有将其保存在数据库中.我通过执行以下操作添加字段:

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

当我运行 rake db:migrate 时没有奇怪的消息,数据库表没问题,表单已被编辑为允许输入姓氏,因为它具有显示和索引视图以正确显示它.但是,它不会将给定的表单值保存到数据库中.

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.

推荐答案

如果您正在使用 Rails4(我认为是),请确保您将此新属性添加到 user_paramscode> 在您的控制器中(因为 strong_parameters):

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

对于这个 Rails 版本的新手来说,这是一个典型的错误.

It's a typical mistake when new to this Rails version.

更多信息:strong_parameters

这篇关于模型中的更改未保存.导轨 4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆