ruby - rails strong parameter error on array attribute -
i try post json rails server request body
{"post":{ "user_id": 2, "title": "kehangatan di pagi hari", "category": "kehangatan di pagi hari", "imageposts": [{"imageurl":"djawhdahwdkjhaw dhjawdhawdawdad"}], "description":"<p>memang sange</p>" } }
and posts_params
def post_params params.require(:post).permit(:title, :user_id, :description, :category, :imageposts => [:imageurl]) end
unfortunately when make ajax post got error in terminal
#<activerecord::associationtypemismatch: imagepost(#42287660) expected, got activesupport::hashwithindifferentaccess(#30074960)>
i've trying imageposts strong parameter doesnt work too
imageposts: [:imageurl]
can solve this...
the strong params
fine. problem imageposts
association, but, guess, tried set attribute post.update_attributes(post_params)
if want updated this, possible solution use accepts_nested_attributes_for
:
your model must have like:
class post belongs_to :user has_many :imageposts accepts_nested_attributes_for :imageposts end
and name of params must imageposts_attributes
instead of imageposts
:
def post_params params.require(:post).permit(:title, :user_id, :description, :category, :imageposts_attributes => [:imageurl]) end
Comments
Post a Comment