Rails添加了不填写表单的模型属性

时间:2016-03-16 11:52:53

标签: mysql ruby-on-rails ruby model attributes

我在模型中添加了一个属性。填写表单字段,但是当我提交时,它不会将其输入到我的数据库中。

该字段是RoleID:

enter image description here

您可以在下一页看到它没有填满数据库:

enter image description here

我也从DB确认:

+----+--------+-------------------------+----------+---------+-------+---------------------+---------------------+---------+
| id | userid | email                   | password | fname   | lname | created_at          | updated_at          | roleids |
+----+--------+-------------------------+----------+---------+-------+---------------------+---------------------+---------+
|  1 |      1 | kendallweihe@gmail.com  | test     | kendall | weihe | 2016-03-15 21:56:30 | 2016-03-15 21:56:30 |    NULL |
|  2 |      2 | kendall.weihe@gmail.com | test     | ken     | weihe | 2016-03-16 11:39:05 | 2016-03-16 11:39:05 |    NULL |
|  3 |      2 | kendall.weihe@gmail.com | test     | ken     | weihe | 2016-03-16 11:42:32 | 2016-03-16 11:42:32 |    NULL |
|  4 |      2 | kendall.weihe@gmail.com | test     | ken     | weihe | 2016-03-16 11:44:26 | 2016-03-16 11:44:26 |    NULL |
+----+--------+-------------------------+----------+---------+-------+---------------------+---------------------+---------+

输入表单如下所示:

  <div class="field">
    <%= f.label :lname %><br>
    <%= f.text_field :lname %>
  </div>
  <div class="field">
    <%= f.label :roleids, "RoleID: 1 for Employee or 2 for Customer" %><br>
    <%= f.number_field :roleids %>
    <%debugger%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

上面的调试器确认该属性存在:

#<User id: nil, userid: nil, email: nil, password: nil, fname: nil, lname: nil, created_at: nil, updated_at: nil, roleids: nil>

控制器操作如下所示:

  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

1 个答案:

答案 0 :(得分:1)

检查您是否在控制器的强参数中添加了roleids属性,如:

def user_params
  params.require(:user).permit(:email, :password, :password_confirmation, :fname, :lname, :roleids)
end
相关问题