【发布时间】:2015-12-30 00:02:18
【问题描述】:
我正在尝试使用 Rails 4 制作应用程序。
我正在使用专家与设计。
我正在尝试在 Pundit 中编写地址策略。
我有三个相关的模型,分别是 user.rb、profile.rb 和 address.rb。
这些关联是:
用户.rb
has_one :profile
profile.rb
belongs_to :user
has_many :addresses, as: :addressable
地址.rb
belongs_to :addressable, :polymorphic => true
我的地址政策是:
class AddressPolicy < ApplicationPolicy
def create?
user.present? && user == address.profile.user
end
def update?
user.present? && user == address.profile.user
end
def destroy?
user.admin?
end
private
def address
record
end
end
我很难弄清楚如何编写此政策。地址是多态的。个人资料可以有许多地址,个人资料属于一个用户。因此,对于策略,我希望拥有配置文件的用户创建和更新地址。我正在苦苦挣扎,因为我不知道如何将个人资料放入关联链中以链接用户和地址。
此策略的当前错误方法以 create?功能:
def create?
user.present? && user == address.profile.user
end
消息是:
undefined method `profile' for #<Address:0x007fbad7ac0cf8>
我的地址策略继承自我的应用程序策略,它初始化用户和记录:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
任何人都可以看到如何做到这一点?
【问题讨论】:
标签: ruby-on-rails devise associations polymorphic-associations pundit