Ruby on Rails Parameter has been sent to Controller but hasn't saved in DB












-2














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.



enter image description here



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.



enter image description here










share|improve this question


















  • 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 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


















-2














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.



enter image description here



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.



enter image description here










share|improve this question


















  • 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 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
















-2












-2








-2







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.



enter image description here



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.



enter image description here










share|improve this question













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.



enter image description here



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.



enter image description here







ruby-on-rails ruby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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
















  • 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 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










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














1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer





















  • Thanks!! It works!! Also thanks for advise in password
    – jeongho
    Dec 27 at 13:52











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
});


}
});














draft saved

draft discarded


















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









1














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.






share|improve this answer





















  • Thanks!! It works!! Also thanks for advise in password
    – jeongho
    Dec 27 at 13:52
















1














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.






share|improve this answer





















  • Thanks!! It works!! Also thanks for advise in password
    – jeongho
    Dec 27 at 13:52














1












1








1






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas