프로젝트
[루비온레일즈] Parameter missing. Create가 안되는 현상
jhkang-dev
2018. 12. 27. 02:13
안녕하세요 강정호입니다. 레일즈 프로젝트를 하면서 생기는 이슈를 적어보겠습니다.
위와 같이 3개의 파라메터를 서버에 보냈습니다.
그러니 다음과 같은 에러메시지를 받았습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Started POST "/users" for 127.0.0.1 at 2018-12-27 02:04:57 +0900 (2.4ms) 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 (2.5ms) 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"}} Completed 500 Internal Server Error in 1ms (ActiveRecord: 4.3ms) ArgumentError (wrong number of arguments (given 1, expected 3)): app/models/user.rb:12:in `initialize' app/controllers/users_controller.rb:18:in `create' | cs |
인수가 3개여야 하는데 1개만 받았다는 메시지가 나타나있네요.
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
user.rb 의 코드는 다음과 같습니다.
class User < ApplicationRecord
attr_accessor :email, :pwd, :usertype
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[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
def initialize(email, pwd, usertype)
@user.email=email, @user.pwd=pwd, @user.usertype=usertype
end
end
user.rb에서의 initialize 때문에 그런것 같아서 initialize를 지우고 다시 서버 호출을 했습니다. 그랫더니 이번에는 다음과 같은 결과가 나왔습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Started POST "/users" for 127.0.0.1 at 2018-12-27 02:10:56 +0900 (2.7ms) 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.6ms) 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.4ms) BEGIN �넶 app/controllers/users_controller.rb:20 User Exists (0.7ms) 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-26 17:10:57', '2018-12-26 17:10:57') �넶 app/controllers/users_controller.rb:20 (55.8ms) COMMIT �넶 app/controllers/users_controller.rb:20 Rendering users/show.json.jbuilder Rendered users/_user.json.jbuilder (0.9ms) Rendered users/show.json.jbuilder (338.7ms) Completed 201 Created in 1309ms (Views: 1215.3ms | ActiveRecord: 87.6ms) | cs |
이번에는 파라메터들이 insert가 되었습니다. 하지만 데이터베이스에서 보면
이와 같이 email, pwd, usertype 부분이 null로 들어가 있습니다.
parameter를 제대로 받지 못해서 생기는 문제인거 같습니다.