Hello, I'm 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 N < - >
The problem is that when sending the JSON of the post with Passion IDs that I want to associate with it, I can not 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 api '/ feed / posts' to create a post specifying several Passions. The JSON I am sending is as follows.
{
"title": "Titulo da postagem",
"description":"Decrição do post",
"passion_ids":[1,2]
}
Sending this post when looking at the log of the rails that receives the request the attribute 'passion_ids' is not sending inside post so I can allow it.
Log when submitting 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 that way but it's in rails 4.2 and the forms are made by forms and not by REST.