ChristopherW
ChristopherW

Reputation: 1063

Ruby on rails Active Records model not saving to database when needed

I am creating sign up logic for a website and am using a form_for to grab all data from the sign up page. I know the data is coming in since I am able to do basic data filtering on the input in the controller. The data coming is as ruby shows is below.

{"utf8"=>"✓",
 "authenticity_token"=>"NOT SHOWN",
 "email"=>"asdf@",
 "password"=>"[FILTERED]",
 "confirmed_password"=>"[FILTERED]",
 "account_type"=>"selection1",
 "submit"=>"Sign Up"}

Once the data is in I do basic data checking in the controller as shown below.

if params[:email].present? && params[:password].present? && 
  params[:confirmed_password].present?
  #check matching passwords and valid email syntax

  if params[:email].include?('@') && params[:password] == params[:confirmed_password]                      && (params[:account_type] == 'tenant' || params[:account_type] == 'pm')
    #passwords match so account can be created

    @newuser = User.new
    @newuser.user_username = params[:email]
    @newuser.user_hash = params[:email]
    @newuser.user_last_update = Time.now
    @newuser.save          <PROBLEM HAPPENS HERE>

    @newemail = Email.new
    @newemail.email_address = params[:email]
    @newemail.email_parent_id = @newuser.user_id
  else
    #passwords do not match
    flash[:notice] = "PASSWORDS DO NOT MATCH"
  end  
else
  #if any of the fields are empty
  flash[:notice] = "FORM MISSING INPUT"
end

The problem is that once it reaches the line where it saves the newuser it throws this error.

Mysql2::Error: Column 'user_hash' cannot be null: INSERT INTO `users`   
(`user_dateofbirth`, `user_firstname`, `user_hash`, `user_id`, `user_last_update`, 
`user_lastname`, `user_middlename`, `user_name`) VALUES (NULL, NULL, NULL, NULL, NULL, 
NULL, NULL, NULL)

I have set that four of the columns cannot be null. user_hash, user_id, user_username and user_last_updated. What I'm confused by is that I set all of the values that cannot be null in the controller, but when I save them it doesn't actually set the values. They all stay null and it just tells me they can't be null.

So, am I not creating a new row in the table correctly or something similar. I am fairly new to Ruby on Rails so I'm stumped.

Almost forgot, here is the model for the user as well. I'm not sure if I need to define more in here to get everything to work.

class User < ActiveRecord::Base
# attr_accessible :title, :body
attr_accessor :user_id, :user_username, :user_last_update, :user_hash
end

-

-

UPDATE:

Here are the calls to set the user_params then add the values to newuser.

    @newuser = User.new(user_params)
    @newuser.user_username = params[:email]
    @newuser.user_hash = params[:email]
    @newuser.user_last_update = Time.now
    @newuser.save

Here is the code for the user_params method

def user_params
params.require(:newuser).permit(:user_username, :user_hash, :user_last_update)
end

The exact error being thrown is below

 TypeError in LoginController#signup

can't convert Symbol into String

Rails.root: /home/rezze-dev/Desktop/Rezze
Application Trace | Framework Trace | Full Trace

app/controllers/login_controller.rb:57:in `user_params'
app/controllers/login_controller.rb:27:in `signup'

This error occurred while loading the following files:
newuser

Line 57 is the params.require line in user_params

Upvotes: 0

Views: 2096

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

Your problem is here: VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

Your model literally has no data being passed to it


Whitelisting

I would say the problem is to do with how you're constructing the ActiveRecord object:

@newuser = User.new(user_params) #-> you'll need to whitelist the params

....

private

def user_params
    params.require(:user).permit(:email, :password, :confirmed_password, :account_type, :user_username, :user_hash, :user_last_update)
end

More specifically, Rails uses strong params to whitelist particular params for the model. If you don't whitelist the various attributes you wish to pass, it just won't allow them through. This is what I believe has happened here

Adapting my code above should work for you - I am available if you leave a comment


Update

Do this:

#app/models/user.rb
Class User < ActiveRecord::Base
   # -> notice nothing here. No need for attr_accessor
end

#app/controllers/users_controller.rb
Class UsersController < ApplicationController
   def some_method
       ... #-> some code
       @user = User.new(user_params)
   end

   private

   def user_params
       parmas.require(:user).permit(:email, :password, :confirmed_password, :account_type, :user_username, :user_hash, :user_last_update)
   end
end 

Upvotes: 2

Related Questions