Ruby on rails 数组属性上的rails强参数错误

Ruby on rails 数组属性上的rails强参数错误,ruby-on-rails,ruby,activerecord,active-model-serializers,Ruby On Rails,Ruby,Activerecord,Active Model Serializers,我尝试用这个请求体将我的json发布到rails服务器 {"post":{ "user_id": 2, "title": "kehangatan di pagi hari", "category": "kehangatan di pagi hari", "imageposts": [{"imageurl":"DJAWHDAHWDKJHAW DHJAWDHAWDAWDAD"}], "description":"<p>memang sang

我尝试用这个请求体将我的json发布到rails服务器

    {"post":{
    "user_id": 2,
    "title": "kehangatan di pagi hari",
    "category": "kehangatan di pagi hari",
    "imageposts": [{"imageurl":"DJAWHDAHWDKJHAW DHJAWDHAWDAWDAD"}],
    "description":"<p>memang sange</p>"
  } }
不幸的是,当我发布ajax帖子时,我的终端出现了一个错误

#<ActiveRecord::AssociationTypeMismatch: Imagepost(#42287660) expected, got ActiveSupport::HashWithIndifferentAccess(#30074960)>

有人能解决这个问题吗?

强参数可以。问题是,
imageposts
是一个关联,但只是猜测,它试图设置为属性
post.update\u attributes(post\u参数)

如果您希望它像这样更新,可能的解决方案是为使用
接受\u嵌套的\u属性\u:

您的模型必须具有以下内容:

class Post
  belongs_to :user
  has_many :imageposts
  accepts_nested_attributes_for :imageposts
end
参数的名称必须是
imageposts\u attributes
,而不仅仅是
imageposts

def post_params
  params.require(:post).permit(:title, :user_id, :description, :category, :imageposts_attributes => [:imageurl])
end

您正在传递多个图像。因此,您可以将
coocoon gem
一起使用,为


有关更多信息,请发布收到的
params
(请参阅服务器日志)其工作,但其回复=>{“imageposts.Post”:[“必须存在”]}。虽然我有一个post\u id属性,但请在post模型中尝试
has\u many:imageposts,reverse\u of::post
。让我们看看是否有帮助
class Post
  belongs_to :user
  has_many :imageposts
  accepts_nested_attributes_for :imageposts
end
def post_params
  params.require(:post).permit(:title, :user_id, :description, :category, :imageposts_attributes => [:imageurl])
end