Reputation: 301
I'm getting above error when passing data through simple_form_for. My view, controller and database file is included below. This looks like I am not getting the values submitted by simple_form_for in controller. Note that, in my database table, attribute status
is not null
and I am not taking it as an input in view. How can I solve this error?
Database
create_table "users", force: :cascade do |t|
t.integer "team_lead_id"
t.string "name", null: false
t.string "email", null: false
t.string "password", null: false
t.date "joining_date", null: false
t.integer "status", null: false
t.integer "role"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
View
<%= simple_form_for @user , :url => user_createUser_url, :method => :post do |f| %>
<%= f.input :name %><br />
<%= f.input :email%><br />
<%= f.input :password %><br />
<%= f.input :joining_date, as: :date, order: [:day, :month, :year] %><br/>
<%= f.submit "Create User" %>
<% end %>
Controller
def createUser
fresh = User.new
fresh.name = params[:name]
fresh.email = params[:email]
fresh.password = params[:password]
fresh.joining_date = params[:joining_date]
fresh.status = 1
fresh.role = 3
if fresh.save
flash[:notice] = "User Created"
redirect_to(:action => index)
else
flash[:notice] = "Creating failed"
redirect_to(:action => index)
end
end
And I'm getting this Error
PG::NotNullViolation: ERROR: null value in column "name" violates not-null constraint DETAIL: Failing row contains (12, null, null, null, null, null, 1, 3, 2018-11-12 13:37:54.589835, 2018-11-12 13:37:54.589835). : INSERT INTO "users" ("status", "role", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"
Upvotes: 1
Views: 1072
Reputation: 11226
createUser
is not a CRUD convention, instead use create
but also you should be using strong parameters and also don't use goofy names like fresh
but instead model objects should be named semantically.
def create
user = User.new(permitted_params)
user.status = 1
user.role = 3
if user.save
flash[:notice] = "User Created"
redirect_to(:action => index)
else
flash[:notice] = "Creating failed"
redirect_to(:action => index)
end
end
def permitted_params
params.require(:user).permit(:name, :email, :password, :joining_date)
end
You might not need joining_date
, since you have created_at
, but if you do want to keep that field, at least use the name joined_date
or join_date
since it will always be past tense. In fact a user is never "joining", they either have or have not "joined" yet.
As a side note you should never store passwords unencrypted, perhaps see bcrypt
Upvotes: 3