Mass assigment

Par Mr Moody, il y a 10 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.

Je suis en train de créer une petite api rest.
j'essaye d'envoyer directement un json comportant un tableau.

{"post": [{ "title":"sedond", "body":"text first", "user_id":2}, { "title":"third", "body":"text third", "user_id":3} ]}

j'essaye d'integrer ceci directement dans ma base

voici mon code rails

class PostsController < ApplicationController before_action :set_post, only: [:show, :update, :destroy] def index @posts = Post.all render json: @posts end def show render json: @post end def create @post = Post.new(post_params) if @post.save render json: @post, status: :created, location: @post else render json: @post.errors, status: :unprocessable_entity end end def update @post = Post.find(params[:id]) if @post.update(post_params) head :no_content else render json: @post.errors, status: :unprocessable_entity end end def destroy @post.destroy head :no_content end private def set_post @post = Post.find(params[:id]) end def post_params params.require(:post).permit(:title, :body, :user_id) end end

Ce que j'obtiens

une erreur me disant que je ne peux passer d'array, comment puis je faire un assignement de masse, dans rails ??

Aucune réponse