[英]rails strong parameter error on array attribute
我尝试使用此请求正文将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>"
} }
这是我的posts_params
def post_params
params.require(:post).permit(:title, :user_id, :description, :category, :imageposts => [:imageurl])
end
不幸的是,当我发布ajax帖子时,我的终端出现错误
#<ActiveRecord::AssociationTypeMismatch: Imagepost(#42287660) expected, got ActiveSupport::HashWithIndifferentAccess(#30074960)>
我已经尝试过这对我的imageposts强参数,但它也无法正常工作
imageposts: [:imageurl]
谁能解决这个问题...
strong params
很好。 问题是imageposts
是一个关联,但是,仅是一个猜测,尝试将其设置为属性post.update_attributes(post_params)
如果您希望像这样进行更新,则可能的解决方案是使用accepts_nested_attributes_for
:
您的模型必须具有以下内容:
class Post
belongs_to :user
has_many :imageposts
accepts_nested_attributes_for :imageposts
end
并且params的名称必须是imageposts_attributes
而不是imageposts
:
def post_params
params.require(:post).permit(:title, :user_id, :description, :category, :imageposts_attributes => [:imageurl])
end
您正在传递多个图像。 因此,您可以将coocoon gem
与accepts_nested_attributes_for
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.