我正在尝试创建一个用户注册页面,其中包含不同的字段类型,包括出生日期。当我尝试将表单数据保存到postgres数据库时。一切正常,除了出生日期列保存null。
new.html
<%= form_for @user, :url => users_path, method: :post do |f| %>
<input type="hidden" name="page" value="register">
<label>First Name</label><%= f.text_field :first_name ,:class => 'textbox' %><br/><br/>
<label>Last Name</label><%= f.text_field :last_name ,:class => 'textbox' %><br/><br/>
<label>Email</label><%= f.text_field :email, :class => 'textbox' %><br/><br/>
<label>Password</label><%= f.password_field(:password, :class => 'textbox') %><br/><br/>
<label>Confirm Password</label><input type="password" name = "confirm_password" class="textbox" type="password" /><br/><br/>
<label>Date of Birth</label><%= f.text_field :dob, :class => 'textbox' %><br/><br/>
<label>Contact No.</label><%= f.text_field :contactno, :class => 'textbox' %><br/><br/>
<%= f.submit('Register User') %>
<% end %>
用户控制器
@user = User.create(params.require(:user).permit(:first_name, :last_name, :email, :password, :dob, :contactno))
binding.pry
if @user.save
session[:user_name] = @user[:email]
redirect_to :controller => 'dashboards', :action => 'index'
end
另外,我注意到在查看控制台时,insert语句不包括出生日期参数。以下是我在控制台中看到的结果。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Sm6UjUXSVSlg6KUs+UUdSXRRfd8y7f65mhyHbR9NwRnp9OJlifUgqk13yqvk4R4vq2+yIZYr2Br+a9uKTenZvg==", "page"=>"register", "user"=>{"first_name"=>"Sam", "last_name"=>"Brat", "email"=>"sm@gmail.com", "password"=>"[FILTERED]", "dob"=>"09-09-1996", "contactno"=>"1234567890"}, "confirm_password"=>"[FILTERED]", "commit"=>"Register User"}
(0.0ms) BEGIN
SQL (4.0ms) INSERT INTO "users" ("first_name", "last_name", "email", "password", "contactno", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["first_name", "Sam"], ["last_name", "Brat"], ["email", "sm@gmail.com"], ["password", "sm12345@"], ["contactno", 1234567890], ["created_at", "2018-04-16 22:37:39.473682"], ["updated_at", "2018-04-16 22:37:39.473682"]]
(0.0ms) COMMIT
提前多多感谢。