【发布时间】:2023-03-24 10:35:01
【问题描述】:
应用程序控制器有一个辅助方法:current_user 可以从我的所有视图中访问。但 pro_user 在我看来是完全无法访问的。
def current_user
@current_user ||= User.find_by_auth_token( cookies[:auth_token]) if cookies[:auth_token]
rescue ActiveRecord::RecordNotFound
end
helper_method :current_user
def pro_user
@pro_user ||= Subscription.where(:email => current_user.email).pluck(:email)
return @pro_user
rescue ActiveRecord::RecordNotFound
end
helper_method :pro_user
现在在视图中,如果我访问 pro_user,它总是为零。但有趣的是,我在应用程序控制器中也有一个名为 current_user 的方法,它是一个 helper_method。它在视图中工作正常。
请问您能帮帮我吗?
【问题讨论】:
-
我不明白你的查询的用例,
Subscription.where(:email => current_user.email).pluck(:email)?当您已经拥有current_user.email时,为什么要通过触发查询从subscriptions表中提取 相同的电子邮件? -
Kirti current_user.email 在用户成功购买后存储在订阅表中。 pro_user 方法位于应用程序控制器中的 current_user 方法下方,用于检查用户是否是高级用户,即 pro_user。在视图中,我试图查看 pro_user.email.blank 是否?它一直为零。我还在视图中尝试了@pro_user。一切都在应用程序控制器中正确打印,但在视图中为 nil。
-
Kirti,你已经帮了我两次了!所以我希望你能再次对此有所了解:)
-
您的方法可以在视图中访问,问题在于返回 0 条记录的查询。您需要弄清楚为什么没有为 current_user 保存订阅记录。按照微薄的回答。他对你的问题给出了详尽的解释。
标签: ruby-on-rails helpermethods