我有一个注册界面,需要用户名,电子邮件和密码才能注册。但不知何故,当我想要注册一个新用户时,它似乎没有写入数据库。 它将在数据库中创建一个新行,但是为名称和电子邮件分配了NULL,如下所示。
知道我在这里缺少什么吗?
编辑:resp的代码。我的user.rb,user_controller.rb和usershelper.rb
class User < ActiveRecord::Base
has_many :microposts
attr_accessor :name, :email
before_save {self.email = email.downcase if email}
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: {maximum: 255}, format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive:false}
has_secure_password
validates :password, length: {minimum: 6}
end
#
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "welcome to the app!"
redirect_to @user
else
render :new
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
#
module UsersHelper
def gravatar_for(user)
gravatar_id = Digest::MD5::hexdigest(user.email.to_s.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
EDIT2:其他信息
<p id="notice"><%= notice %></p>
<% provide(:title, @user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
</aside>
</div>
<p>
<strong>Name:</strong>
<%= @user.name %>
</p>
<p>
<strong>Email:</strong>
<%= @user.email %>
</p>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
答案 0 :(得分:2)
删除此行:
attr_accessor :name, :email
它覆盖了数据库属性,因此值被写入@name和@email,而不是保存到数据库中。