为什么我的Rails表单中的额外字段不会保存到数据库表中?

时间:2010-11-03 18:11:19

标签: ruby-on-rails activerecord

我有表格

<% form_tag users_path, :id => 'registrationForm' do %>
  <div class="formElement">
    <label for="emailAddress">Email Address</label>
    <input name="user[email]" type="text">
  </div>
  <div class="formElement">
    <label for="password">Password</label>
    <input id="password" name="user[password]" type="password">
  </div>
  <div class="formElement">
    <label for="passwordConfirmation">Re-Enter Password</label>
    <input id="passwordConfirmation" name="user[password_confirmation]" type="password">
  </div>

  <div class="formElement right">
    <input name="commit" value="Create" type="submit">
  </div>
<% end %>

然后我的控制器方法来处理包含

的内容
@user = User.new(params[:user])
@user.save do |result|
  ...
end

这很好用。但是,如果我将'first_name'和'last_name'字段添加到我的数据库表并添加

<div class="formElement">
  <label for="firstName">First Name</label>
  <input id="firstName" name="user[first_name]" type="text" />
</div>
<div class="formElement">
  <label for="lastName">Last Name</label>
  <input id="lastName" name="user[last_name]" type="text" />
</div>

到我的表格,我得到

  

Mysql ::错误:列'last_name'   不能为空:INSERT INTO users   (saltship_address_id,   created_atsingle_access_token,   last_request_atbill_address_id,   crypted_password,   remember_token_expires_at,   updated_atperishable_token,   api_keyfailed_login_count,   current_login_ip,   openid_identifier,   current_login_atlast_name,   remember_tokenpersistence_token,   login_countlast_login_ip,   last_login_atloginemail,   first_name)   VALUES('gPa4FNsPHbfxLz1FTZJ8',NULL,   '2010-11-03 18:07:24',   'INl0QTDduoCKSdLLXEqb','2010-11-03   18:07:24',NULL,   '900d7300768651e4814ca16b1dd39b85e8111c92a63d366c82e3f1d501dc7b85efc060bc2032e55e4405fe33b0883b0ad586fe47e99261b046a34a8b9d785333',   NULL,'2010-11-03 18:07:24',   'r8YSPUfKsmbIIJryvz5C',NULL,0,   '127.0.0.1',NULL,'2010-11-03   18:07:24',NULL,NULL,   'd4fe33e9c1bde5e2468d74d3dc1de28089f565d7e0d39584690452547be6d3bbf529e9ac118575529f34e377cce315697538c64b19f799e386d6977a8f37912e',   1,NULL,NULL,'testuser3 @test.com',   'testuser3@test.com',NULL)

以下是错误页面所说的参数:

  

{ “提交”=&gt; “中创建”,   “authenticity_token”=&gt; “中tFpn + DGMU3VfeaSrc5ckVoVCxfy76Xm0Mqf8Jx8JEFs =”,   “用户”=&GT; { “password_confirmation”=&gt; “中的测试”,   “姓氏”=&gt; “中用户3”,   “密码”=&gt; “中的测试”,   “如first_name”=&gt; “中Test3的”,   “电子邮件”=&gt; “中testuser3@test.com”}}

为什么我会收到这个MySQL错误?

1 个答案:

答案 0 :(得分:3)

您是否在User模型中使用attr_accessibleattr_protected来防范群体分配攻击?