Rails Michael Hartl教程+设计current_user? [英] Rails Michael Hartl Tutorial + Devise current_user?
问题描述
我正在尝试在我的rails作曲器devise应用中实施M. Hartl教程中的以下操作。
I am trying to implement the follow actions from M. Hartl's tutorial in my rails composer, devise app.
但是由于某种原因,我会收到此错误
But for some reason i get this error
undefined method `current_user?' for
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
知道为什么吗?
谢谢
推荐答案
Devise默认为您提供 current_user
助手。但是,它没有给您 current_user?
布尔方法。您需要在 application_helper.rb
中自己定义此方法:
Devise gives you the current_user
helper by default. However, it doesn't give you current_user?
boolean method. You need to define this method yourself in application_helper.rb
:
def current_user?(user)
user == current_user
end
现在 current_helper?
定义了您可以传入 @user
实例变量
Now you have current_helper?
defined you can pass in your @user
instance variable
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
这就是您要做的一切。您无需以任何方式触摸您的 ApplicationController或SessionsHelper
。您只需使用Devise提供的 current_user
来定义 current_user?(user)
。这种方式更容易实现。希望这会有所帮助
Thats all you have to do. You don't have to touch your ApplicationController or SessionsHelper
in any way. You are simply using current_user
which Devise gives you in order to define current_user?(user)
. This way is much easier to implement. Hope this helps
这篇关于Rails Michael Hartl教程+设计current_user?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!