我正在尝试更新一个属性。它是一个默认值为 false 的 bool 值。在我的方法中,我进行查询以找到合适的用户。然后我尝试通过翻转 owner boolean
来更新该用户为真。这是我遇到的一个非常奇怪的错误,因为它正在更新方法但它也发出此错误 TypeError: nil is not a symbol nor a string
我只是想知道我在这里做错了什么。
Controller
def update
membership = current_account.account_memberships.find_by(user_id: params[:id])
membership.update(owner: true)
end
HTML<%= link_to "Owner", account_membership_path(user), {
class: "icon icon-owner-upgrade",
method: :patch,
} %>
最佳答案
如果您尝试更新依赖模型中的记录,它可能只有一个外键,而没有主键。添加一行self.primary_key
即可解决问题像这样:
class YourDependentRecord < ApplicationRecord
self.primary_key = 'user_id'
end
哪里'user_id'
是主模型中的主键。
关于ruby-on-rails - Rails - 更新时类型错误 : nil is not a symbol nor a string,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40623598/