欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

rails cancan AssociationTypeMismatch

程序员文章站 2022-03-24 12:01:06
...
ActiveRecord::AssociationTypeMismatch (User(#70244306330580) expected, got String(#70244198919200)):
  activerecord (3.2.13) lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
  activerecord (3.2.13) lib/active_record/associations/belongs_to_association.rb:6:in `replace'
  activerecord (3.2.13) lib/active_record/associations/singular_association.rb:17:in `writer'
  activerecord (3.2.13) lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
  activerecord (3.2.13) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
  activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78:in `each'
  activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
  activerecord (3.2.13) lib/active_record/base.rb:498:in `initialize'
  state_machine (1.2.0) lib/state_machine/integrations/active_record.rb:470:in `initialize'


如果遇到这个问题,那么请检查请求parameters里面是不是有一个Key-Value为"id": "",  如果有,请去掉。我这里用了cancan,配置了安全验证后,每个action都会去首先去取id为params[:id]的User。

代码:https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/base_helpers.rb#L51
def resource
        get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_find, params[:id]))
      end
相关标签: activerecord cancan