Home Save method does not store date of birth in the database in Rails
Reply: 0

Save method does not store date of birth in the database in Rails

user2831
1#
user2831 Published in April 22, 2018, 10:10 pm

I am trying to make a user registration page where I have different field types including date of birth. When I try to save the form data into the postgres database. Everything works except date of birth column saves 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 controller

@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

Also, I've noticed that while looking at the console, the insert statement does not include date of birth parameter. Below is the result I see in console.

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

Thanks a lot in advance.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302639 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO