POSTテーブルのレコードを作る際にtitle
とcontent
のデータが入ったものを作ろうとしています。
パラメーターは送られているんですが、作られたデータにはtitle
とcontent
がnullになってしまっています。
コンソールからはちゃんと作れるみたいです。。
ご教授お願いします泣
rails cでつくったとき
[3] pry(main)> Post.create(title: "aaa", content:"ccc") (0.2ms) BEGIN Post Create (2.9ms) INSERT INTO `posts` (`title`, `content`, `created_at`, `updated_at`) VALUES ('aaa', 'ccc', '2019-07-13 06:29:05', '2019-07-13 06:29:05') (3.9ms) COMMIT => #<Post:0x00007fa98aa91dd0 id: 3, title: "aaa", content: "ccc", created_at: Sat, 13 Jul 2019 06:29:05 UTC +00:00, updated_at: Sat, 13 Jul 2019 06:29:05 UTC +00:00>
postmanからパラメーターおくったとき
Started POST "/api/posts/?post[title]=aaa&post[content]=22" for ::1 at 2019-07-13 15:44:02 +0900 Processing by Api::PostsController#create as */* Parameters: {"post"=>{"title"=>"aaa", "content"=>"22"}} (22.3ms) BEGIN ↳ app/controllers/api/posts_controller.rb:16 Post Create (49.0ms) INSERT INTO `posts` (`created_at`, `updated_at`) VALUES ('2019-07-13 06:44:02', '2019-07-13 06:44:02') ↳ app/controllers/api/posts_controller.rb:16 (18.0ms) COMMIT ↳ app/controllers/api/posts_controller.rb:16 [active_model_serializers] Rendered PostSerializer with ActiveModelSerializers::Adapter::Attributes (11.14ms) Completed 200 OK in 112ms (Views: 11.9ms | ActiveRecord: 89.3ms)
posts_controller.rb
class Api::PostsController < ApplicationController before_action :set_post, only: [:show, :update, :destroy] def index @posts = Post.all render json: @posts, serializer: PostSerializer, root: nil end def show render json: @post, serializer: PostSerializer, root: nil end def create @post = Post.new(post_params) if @post.save render json: @post, serializer: PostSerializer, root: nil else render json: { status: 'error', data: @post.errors } end end def update @post.update(post_params) if @post.save render json: @post, serializer: PostSerializer, root: nil else render json: { status: 'error', data: @post.errors } end end def delete @post.destroy return render_error('Post_item not found.', 400) if @post == false render json: @post, serializer: PostSerializer, root: nil end private def post_params params.require(:post).permit(:title, :content) end def set_post @post = Post.find(params[:id]) end end
回答1件
あなたの回答
tips
プレビュー