Many to Many JSON POST - Rails 5 - Only API

Asked

Viewed 262 times

3

Hello, I am developing an engine in Rails 5 where it will be just a blog API. It will be a simple system. Post has several Passions and Passions has several Posts. I made the relationship N <->

The problem is that by sending the post JSON with the Passions Ids that I want to associate with it I can’t save them.

Post.Rb

module Feed
  class Post < ApplicationRecord
    has_many :post_passions,  dependent: :destroy 
    has_many :passions, :through => :post_passions
  end
end

Passion.Rb

module Feed
  class Passion < ApplicationRecord
    has_many :post_passions
    has_many :post, :through => :post_passions
  end
end

Postpassion.Rb (Join Table)

module Feed
  class PostPassion < ApplicationRecord
     belongs_to :passion
     belongs_to :post
  end
end

My goal is through a POST request in the '/feed/posts' api to be able to create a post specifying several Passions. The JSON I am sending is the following.

{
  "title": "Titulo da postagem",
  "description":"Decrição do post",
  "passion_ids":[1,2]
}

Sending this post while looking at the log of the Rails that receives the request the attribute 'passion_ids' is not sending in post so I can allow it.

Log when sending the request

Started POST "/feed/posts" for 127.0.0.1 at 2017-03-29 08:20:00 -0300
Processing by Feed::PostsController#create as */*
Parameters: {"title"=>"Titulo da postagem", "description"=>"Decrição do post", "passion_ids"=>[1, 2], "post"=>{"title"=>"Titulo da postagem", "description"=>"Decrição do post"}}

As the 'passion_ids' is not received within Post the Permit below does not work.

params.require(:post).permit(:title,
                             :passion_ids,
                             :description)

I have a system that works this way but it is in Rails 4.2 and the registrations are made by forms and not by REST.

3 answers

2


I think you have some questions there. First, I think your JSON request should have a root, in case:

{
    "post": {
        "title": "Titulo da postagem",
        "description":"Decrição do post",
        "passion_ids":[1,2]
    }
}

Second, if you want to pass an array as an attribute, you should allow it differently, like this:

params.require(:post).permit(:title,
                             :description,
                             passion_ids: [])

So Rails knows you’re going to pass an array. I hope I helped.

0

To receive the passion_ids must have this attribute passion_ids in model.post

params.require(:post).permit(:title, :passion_ids, :description)

0

I believe the error is here

params.require(:post).permit(:title,:passion_ids,:description)

your ':passion_ids' is as an attribute of :post, while it is being passed at the root of the parameters by the request.

  • So, but for me to be able to register the Passions together with the Post these data should come together with Post. They come together when post = Post.new(params_post) / post.save() I would already save them with all Passions arrows.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.