在Rails中,可以通过以下方式从通知中访问current_user:
```ruby
class Post < ApplicationRecord
before_create :set_current_user
private
def set_current_user
self.user = current_user
end
end
```
```ruby
class Notification < ApplicationRecord
before_create :set_current_user
private
def set_current_user
self.user = current_user
end
end
```
<p>通知内容</p>
<p>发送者: <%= @notification.sender.name %></p>
<p>接收者: <%= @notification.user.name %></p>
上述示例中,通过调用@notification.user.name来获取通知的接收者的用户名。
总结起来,通过在通知的发送者和接收者模型中设置回调方法,将current_user的值赋给通知对象的user属性,就可以在通知中访问current_user了。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云