0

我在 railscasts 中关注了有关巫术的所有内容,但是当我尝试创建用户时,它给了我这个错误

SQLite3::ConstraintException: users.username may not be NULL: INSERT INTO "users" ("created_at", "crypted_password", "email", "salt", "updated_at") VALUES (?, ?, ?, ?, ?)

请帮我解决这个问题,请去看看我的 github:https ://github.com/simpsonness/test_app

谢谢~~~

4

2 回答 2

1

消息表示您尚未username为新用户记录设置字段。有办法解决它。在您的app/views/users/new.html.erb文件中,您需要添加一个username字段。因此,它将如下所示:

<h1>Sign Up</h1>

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :username %>
    <%= f.text_field :username %>
  </div>
  <div class="field">
    <%= f.label :email %>
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %>
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions"><%= f.submit %></div>
<% end %>

此外,您需要修复app/controllers/users_controller.rb. 用这个更新你的user_params方法:

def user_params  
  params.require(:user).permit(:username, :email, :password, :password_confirmation, :remember_me)
end

就是这样!它现在应该可以工作了。

于 2014-01-24T14:10:52.833 回答
0

如果您不想使用username列,请创建一个将其删除的迁移:

bundle exec rails g migration remove_username_from_users username:string

并运行它。否则,在您的私有方法中添加:username允许的参数并将适当的字段添加到您的视图中。UsersController#user_params

于 2014-01-24T14:04:48.903 回答