Ruby on Rails Parameter has been sent to Controller but hasn't saved in DB
I sent the parameter from Postman. Parameters are successfully sent to Server. but Controller doesn't save it into Database.
I tried to make initializer in User.rb but it doesn't works with initializer "Parameter is missed. given 1, expected 3" error message.
So I erased initializer and Server can receive the parameters well but cannot save in database.
User.rb
class User < ApplicationRecord
attr_accessor :email, :pwd, :usertype
=begin
def initialize(email, pwd, usertype) @email=email, @pwd=pwd, @usertype=usertype end=end
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
validates :email, presence: true, uniqueness: { case_sensitive: false},
format: {with: VALID_EMAIL_REGEX}, length: {minimum:3, maximum:50}
validates :pwd, presence: true, length: {minimum:3, maximum:25}
has_many :bookings
end
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]
# GET /users
# GET /users.json def index
@users = User.all
end
# GET /users/1
# GET /users/1.json def show
end
# POST /users
# POST /users.json def create
@user = User.new(user_params)
if @user.save
render :show, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json def update
if @user.update(user_params)
render :show, status: :ok, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# DELETE /users/1
# DELETE /users/1.json def destroy
@user.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :pwd, :usertype)
# params.permit!
end
end
routes.rb
Rails.application.routes.draw do
resources :bookings
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
#
end
Server message
Started POST "/users" for 127.0.0.1 at 2018-12-27 21:53:13 +0900
(2.6ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
�넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
(1.0ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
�넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by UsersController#create as */*
Parameters: {"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan", "user"=>{"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan"}}
(0.5ms) BEGIN
�넶 app/controllers/users_controller.rb:20
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'admin2@namver.com' LIMIT 1
�넶 app/controllers/users_controller.rb:20
User Create (2.0ms) INSERT INTO `users` (`created_at`, `updated_at`) VALUES ('2018-12-27 12:53:13', '2018-12-27 12:53:13')
�넶 app/controllers/users_controller.rb:20
(60.1ms) COMMIT
�넶 app/controllers/users_controller.rb:20
Rendering users/show.json.jbuilder
Rendered users/_user.json.jbuilder (0.8ms)
Rendered users/show.json.jbuilder (280.3ms)
Completed 201 Created in 1431ms (Views: 1299.2ms | ActiveRecord: 102.5ms)
As you can see, server received paramater well. but Controller insert only created_at, updated_at date column.
ruby-on-rails ruby
add a comment |
I sent the parameter from Postman. Parameters are successfully sent to Server. but Controller doesn't save it into Database.
I tried to make initializer in User.rb but it doesn't works with initializer "Parameter is missed. given 1, expected 3" error message.
So I erased initializer and Server can receive the parameters well but cannot save in database.
User.rb
class User < ApplicationRecord
attr_accessor :email, :pwd, :usertype
=begin
def initialize(email, pwd, usertype) @email=email, @pwd=pwd, @usertype=usertype end=end
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
validates :email, presence: true, uniqueness: { case_sensitive: false},
format: {with: VALID_EMAIL_REGEX}, length: {minimum:3, maximum:50}
validates :pwd, presence: true, length: {minimum:3, maximum:25}
has_many :bookings
end
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]
# GET /users
# GET /users.json def index
@users = User.all
end
# GET /users/1
# GET /users/1.json def show
end
# POST /users
# POST /users.json def create
@user = User.new(user_params)
if @user.save
render :show, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json def update
if @user.update(user_params)
render :show, status: :ok, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# DELETE /users/1
# DELETE /users/1.json def destroy
@user.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :pwd, :usertype)
# params.permit!
end
end
routes.rb
Rails.application.routes.draw do
resources :bookings
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
#
end
Server message
Started POST "/users" for 127.0.0.1 at 2018-12-27 21:53:13 +0900
(2.6ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
�넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
(1.0ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
�넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by UsersController#create as */*
Parameters: {"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan", "user"=>{"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan"}}
(0.5ms) BEGIN
�넶 app/controllers/users_controller.rb:20
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'admin2@namver.com' LIMIT 1
�넶 app/controllers/users_controller.rb:20
User Create (2.0ms) INSERT INTO `users` (`created_at`, `updated_at`) VALUES ('2018-12-27 12:53:13', '2018-12-27 12:53:13')
�넶 app/controllers/users_controller.rb:20
(60.1ms) COMMIT
�넶 app/controllers/users_controller.rb:20
Rendering users/show.json.jbuilder
Rendered users/_user.json.jbuilder (0.8ms)
Rendered users/show.json.jbuilder (280.3ms)
Completed 201 Created in 1431ms (Views: 1299.2ms | ActiveRecord: 102.5ms)
As you can see, server received paramater well. but Controller insert only created_at, updated_at date column.
ruby-on-rails ruby
2
Are you really going to store the password in plain text into your database?
– spickermann
Dec 27 at 13:39
Why do you have thoseattr_accessor
andinitialize
methods in the model? Rails should already dynamically define methods relating to table column names; I believe the issue is that you're overriding them with these custom ones that "do nothing". in terms of database transactions.
– Tom Lord
Dec 27 at 13:41
add a comment |
I sent the parameter from Postman. Parameters are successfully sent to Server. but Controller doesn't save it into Database.
I tried to make initializer in User.rb but it doesn't works with initializer "Parameter is missed. given 1, expected 3" error message.
So I erased initializer and Server can receive the parameters well but cannot save in database.
User.rb
class User < ApplicationRecord
attr_accessor :email, :pwd, :usertype
=begin
def initialize(email, pwd, usertype) @email=email, @pwd=pwd, @usertype=usertype end=end
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
validates :email, presence: true, uniqueness: { case_sensitive: false},
format: {with: VALID_EMAIL_REGEX}, length: {minimum:3, maximum:50}
validates :pwd, presence: true, length: {minimum:3, maximum:25}
has_many :bookings
end
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]
# GET /users
# GET /users.json def index
@users = User.all
end
# GET /users/1
# GET /users/1.json def show
end
# POST /users
# POST /users.json def create
@user = User.new(user_params)
if @user.save
render :show, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json def update
if @user.update(user_params)
render :show, status: :ok, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# DELETE /users/1
# DELETE /users/1.json def destroy
@user.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :pwd, :usertype)
# params.permit!
end
end
routes.rb
Rails.application.routes.draw do
resources :bookings
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
#
end
Server message
Started POST "/users" for 127.0.0.1 at 2018-12-27 21:53:13 +0900
(2.6ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
�넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
(1.0ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
�넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by UsersController#create as */*
Parameters: {"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan", "user"=>{"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan"}}
(0.5ms) BEGIN
�넶 app/controllers/users_controller.rb:20
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'admin2@namver.com' LIMIT 1
�넶 app/controllers/users_controller.rb:20
User Create (2.0ms) INSERT INTO `users` (`created_at`, `updated_at`) VALUES ('2018-12-27 12:53:13', '2018-12-27 12:53:13')
�넶 app/controllers/users_controller.rb:20
(60.1ms) COMMIT
�넶 app/controllers/users_controller.rb:20
Rendering users/show.json.jbuilder
Rendered users/_user.json.jbuilder (0.8ms)
Rendered users/show.json.jbuilder (280.3ms)
Completed 201 Created in 1431ms (Views: 1299.2ms | ActiveRecord: 102.5ms)
As you can see, server received paramater well. but Controller insert only created_at, updated_at date column.
ruby-on-rails ruby
I sent the parameter from Postman. Parameters are successfully sent to Server. but Controller doesn't save it into Database.
I tried to make initializer in User.rb but it doesn't works with initializer "Parameter is missed. given 1, expected 3" error message.
So I erased initializer and Server can receive the parameters well but cannot save in database.
User.rb
class User < ApplicationRecord
attr_accessor :email, :pwd, :usertype
=begin
def initialize(email, pwd, usertype) @email=email, @pwd=pwd, @usertype=usertype end=end
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
validates :email, presence: true, uniqueness: { case_sensitive: false},
format: {with: VALID_EMAIL_REGEX}, length: {minimum:3, maximum:50}
validates :pwd, presence: true, length: {minimum:3, maximum:25}
has_many :bookings
end
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]
# GET /users
# GET /users.json def index
@users = User.all
end
# GET /users/1
# GET /users/1.json def show
end
# POST /users
# POST /users.json def create
@user = User.new(user_params)
if @user.save
render :show, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json def update
if @user.update(user_params)
render :show, status: :ok, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# DELETE /users/1
# DELETE /users/1.json def destroy
@user.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :pwd, :usertype)
# params.permit!
end
end
routes.rb
Rails.application.routes.draw do
resources :bookings
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
#
end
Server message
Started POST "/users" for 127.0.0.1 at 2018-12-27 21:53:13 +0900
(2.6ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
�넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
(1.0ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
�넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by UsersController#create as */*
Parameters: {"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan", "user"=>{"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan"}}
(0.5ms) BEGIN
�넶 app/controllers/users_controller.rb:20
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'admin2@namver.com' LIMIT 1
�넶 app/controllers/users_controller.rb:20
User Create (2.0ms) INSERT INTO `users` (`created_at`, `updated_at`) VALUES ('2018-12-27 12:53:13', '2018-12-27 12:53:13')
�넶 app/controllers/users_controller.rb:20
(60.1ms) COMMIT
�넶 app/controllers/users_controller.rb:20
Rendering users/show.json.jbuilder
Rendered users/_user.json.jbuilder (0.8ms)
Rendered users/show.json.jbuilder (280.3ms)
Completed 201 Created in 1431ms (Views: 1299.2ms | ActiveRecord: 102.5ms)
As you can see, server received paramater well. but Controller insert only created_at, updated_at date column.
ruby-on-rails ruby
ruby-on-rails ruby
asked Dec 27 at 13:33
jeongho
144
144
2
Are you really going to store the password in plain text into your database?
– spickermann
Dec 27 at 13:39
Why do you have thoseattr_accessor
andinitialize
methods in the model? Rails should already dynamically define methods relating to table column names; I believe the issue is that you're overriding them with these custom ones that "do nothing". in terms of database transactions.
– Tom Lord
Dec 27 at 13:41
add a comment |
2
Are you really going to store the password in plain text into your database?
– spickermann
Dec 27 at 13:39
Why do you have thoseattr_accessor
andinitialize
methods in the model? Rails should already dynamically define methods relating to table column names; I believe the issue is that you're overriding them with these custom ones that "do nothing". in terms of database transactions.
– Tom Lord
Dec 27 at 13:41
2
2
Are you really going to store the password in plain text into your database?
– spickermann
Dec 27 at 13:39
Are you really going to store the password in plain text into your database?
– spickermann
Dec 27 at 13:39
Why do you have those
attr_accessor
and initialize
methods in the model? Rails should already dynamically define methods relating to table column names; I believe the issue is that you're overriding them with these custom ones that "do nothing". in terms of database transactions.– Tom Lord
Dec 27 at 13:41
Why do you have those
attr_accessor
and initialize
methods in the model? Rails should already dynamically define methods relating to table column names; I believe the issue is that you're overriding them with these custom ones that "do nothing". in terms of database transactions.– Tom Lord
Dec 27 at 13:41
add a comment |
1 Answer
1
active
oldest
votes
Just remove this line
attr_accessor :email, :pwd, :usertype
from your User
class.
The setter and getter methods generated by attr_accessor
override the getter and setter methods that are created by Rails automatically.
And – for security reasons – please do not store passwords in plaintext into your database. I suggest reading about has_secure_password
.
Thanks!! It works!! Also thanks for advise in password
– jeongho
Dec 27 at 13:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945948%2fruby-on-rails-parameter-has-been-sent-to-controller-but-hasnt-saved-in-db%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just remove this line
attr_accessor :email, :pwd, :usertype
from your User
class.
The setter and getter methods generated by attr_accessor
override the getter and setter methods that are created by Rails automatically.
And – for security reasons – please do not store passwords in plaintext into your database. I suggest reading about has_secure_password
.
Thanks!! It works!! Also thanks for advise in password
– jeongho
Dec 27 at 13:52
add a comment |
Just remove this line
attr_accessor :email, :pwd, :usertype
from your User
class.
The setter and getter methods generated by attr_accessor
override the getter and setter methods that are created by Rails automatically.
And – for security reasons – please do not store passwords in plaintext into your database. I suggest reading about has_secure_password
.
Thanks!! It works!! Also thanks for advise in password
– jeongho
Dec 27 at 13:52
add a comment |
Just remove this line
attr_accessor :email, :pwd, :usertype
from your User
class.
The setter and getter methods generated by attr_accessor
override the getter and setter methods that are created by Rails automatically.
And – for security reasons – please do not store passwords in plaintext into your database. I suggest reading about has_secure_password
.
Just remove this line
attr_accessor :email, :pwd, :usertype
from your User
class.
The setter and getter methods generated by attr_accessor
override the getter and setter methods that are created by Rails automatically.
And – for security reasons – please do not store passwords in plaintext into your database. I suggest reading about has_secure_password
.
answered Dec 27 at 13:41
spickermann
58.5k65576
58.5k65576
Thanks!! It works!! Also thanks for advise in password
– jeongho
Dec 27 at 13:52
add a comment |
Thanks!! It works!! Also thanks for advise in password
– jeongho
Dec 27 at 13:52
Thanks!! It works!! Also thanks for advise in password
– jeongho
Dec 27 at 13:52
Thanks!! It works!! Also thanks for advise in password
– jeongho
Dec 27 at 13:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945948%2fruby-on-rails-parameter-has-been-sent-to-controller-but-hasnt-saved-in-db%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Are you really going to store the password in plain text into your database?
– spickermann
Dec 27 at 13:39
Why do you have those
attr_accessor
andinitialize
methods in the model? Rails should already dynamically define methods relating to table column names; I believe the issue is that you're overriding them with these custom ones that "do nothing". in terms of database transactions.– Tom Lord
Dec 27 at 13:41