gpt4 book ai didi

ruby-on-rails - 如何以及在何处添加 Devise View 辅助方法 current_user bool 值?

转载 作者:行者123 更新时间:2023-12-04 03:40:58 26 4
gpt4 key购买 nike

这是我想在我的 View 中访问的方法:

def current_user?(user)
user == current_user
end

我基本上需要检查当前用户不能在 View 中将自己加为好友和取消加为好友。

<% unless current_user?(@user) %>
<% if current_user.friendly?(@user)
<%= render 'unfriend' %>
<% else %>
<%= render 'friend' %>
<% end %>
<% end %>

current_user 辅助方法已经由 Devise 提供。如何以及在我的 Rails 项目中添加此方法的位置?

谢谢你的帮助

最佳答案

在设计 View 中使用应用程序辅助方法。将您的 class_eval 代码放在 Devise 初始化程序的底部,就在 Devise.setup block 之后。

config/initializers/devise.rb

Devise.setup do |config|
# ... existing Devise configuration, no modification necessary to include helper(s) ...
end

编辑:

Devise::Mailer.class_eval do
helper :application # include "ApplicationHelper", adjust to suit your needs
end

关于ruby-on-rails - 如何以及在何处添加 Devise View 辅助方法 current_user bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16327030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com