【发布时间】:2016-11-18 21:21:58
【问题描述】:
我想询问邮递员的 param post,以便我可以在 rails 的控制器中获取它。
这是我的控制器
class Api::V1::UsersController < ApplicationController
before_action :get_user, only: [:show, :fullname]
#get all user object
def index
render :json => User.all
end
# get user object with id
def show
render :json => @user
end
# get full name of user object with id
def fullname
@hasil = @user.firstname + ' ' + @user.lastname
render :json => @hasil
end
# post object into model
def create
@user = User.new(user_params)
@user.save
if @user.save
render :json => @user
else
render :json => @user.errors.full_messages
end
end
private
def user_params
params.permit(:username, :firstname, :lastname, :age)
end
def get_user
@user = User.find(params[:id])
end
end
当我在邮递员中尝试时,我的用户名、名字、姓氏和年龄都为空。
我是这样发的
[
{
"username": "admin2",
"firstname": "Reza Adha",
"lastname": "Hamonangan",
"age": 23,
}
]
我尝试发帖时的 Rails 服务器日志
Started POST "/api/v1/users" for ::1 at 2016-11-18 18:04:24 +0700
ActiveRecord::SchemaMigration Load (0.0ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by Api::V1::UsersController#create as */*
(0.0ms) BEGIN
SQL (1.0ms) INSERT INTO `users` (`created_at`, `updated_at`) VALUES ('2016-11-18 11:04:25', '2016-11-18 11:04:25')
(4.2ms) COMMIT
(0.0ms) BEGIN
(0.0ms) COMMIT
Completed 200 OK in 22ms (Views: 0.3ms | ActiveRecord: 7.2ms)
我认为我在控制器中做错了从邮递员那里获取 POST DATA。 请帮我.. 感谢您的关注。
【问题讨论】:
-
您可以添加您的服务器日志以进行参数传递,以便更好地跟踪错误吗?
-
Started POST "/api/v1/users" for ::1 at 2016-11-18 18:04:24 +0700 ActiveRecord::SchemaMigration Load (0.0ms) SELECTschema_migrations.* FROMschema_migrations` Api::V1::UsersController#create as / (0.0ms) BEGIN SQL (1.0ms) INSERT INTO @ 987654326@ (created_at,updated_at) 值 ('2016-11-18 11:04:25', '2016-11-18 11:04:25') (4.2ms) COMMIT (0.0ms) BEGIN ( 0.0ms) COMMIT 在 22ms 内完成 200 OK (Views: 0.3ms | ActiveRecord: 7.2ms)` -
这里,您的参数似乎不是从邮递员传递过来的。
-
我编辑了我的帖子,我粘贴了日志以便您轻松查看
-
这里的参数列表是空白的。请检查您的邮递员请求和参数。
标签: ruby-on-rails postman