Ruby on rails Rails Michael Hartl教程+;设计当前用户?
我正试图在我的rails作曲家Desive应用程序中实现M.Hartl教程中的以下操作 但出于某种原因,我得到了这个错误Ruby on rails Rails Michael Hartl教程+;设计当前用户?,ruby-on-rails,devise,Ruby On Rails,Devise,我正试图在我的rails作曲家Desive应用程序中实现M.Hartl教程中的以下操作 但出于某种原因,我得到了这个错误 undefined method `current_user?' for 使用此代码时 <% unless current_user?(@user) %> <div id="follow_form"> <% if current_user.following?(@user) %> <%= render 'unfol
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 %>
知道为什么吗
谢谢更新 希望您具有以下设置: 您的
应用程序控制器
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
# Force signout to prevent CSRF attacks
def handle_unverified_request
sign_out
super
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
def authorize
redirect_to login_url, alert: "Not Autherized" if current_user.nil?
end
def track_activity(trackable, action = params[:action])
current_user.activities.create! action: action, trackable: trackable
end
end
然后,您可以在视图中执行以下操作:
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
更新 希望您具有以下设置: 您的
应用程序控制器
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
# Force signout to prevent CSRF attacks
def handle_unverified_request
sign_out
super
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
def authorize
redirect_to login_url, alert: "Not Autherized" if current_user.nil?
end
def track_activity(trackable, action = params[:action])
current_user.activities.create! action: action, trackable: trackable
end
end
然后,您可以在视图中执行以下操作:
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
默认情况下,designe为您提供
当前用户
助手。但是,它不会为您提供当前用户?
布尔方法。您需要自己在application\u helper.rb
中定义此方法:
def current_user?(user)
user == current_user
end
现在您已经定义了当前\u helper?
,您可以传入@user
实例变量
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
这就是你要做的。您不必以任何方式触摸
应用程序控制器或会话助手。您只需使用design提供给您的当前用户
,即可定义当前用户?(用户)
。这种方法更容易实现。希望这有助于designe默认为您提供当前用户
助手。但是,它不会为您提供当前用户?
布尔方法。您需要自己在application\u helper.rb
中定义此方法:
def current_user?(user)
user == current_user
end
现在您已经定义了当前\u helper?
,您可以传入@user
实例变量
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
这就是你要做的。您不必以任何方式触摸
应用程序控制器或会话助手。您只需使用design提供给您的当前用户
,即可定义当前用户?(用户)
。这种方法更容易实现。希望这有帮助您是否定义了当前用户?
任何地方?我认为Deviate有当前用户
和用户登录
方法,但不是您正在使用的方法。请给章节no.mind.blank,我已经检查过该链接,它给当前_用户的方法与我的代码@bapusethi中的相同,清单11.23。请注意,“当前_用户”和“当前_用户?”不是相同的方法。@user2225129此错误参数数错误(1代表0)这意味着该方法不接受任何参数,而您向其提供的参数从外观上看是@user
您是否在任何地方定义了当前用户?
?我认为Deviate有当前用户
和用户登录
方法,但不是您正在使用的方法。请给章节no.mind.blank,我已经检查过该链接,它给当前_用户的方法与我的代码@bapusethi中的相同,清单11.23。请注意,“当前_用户”和“当前_用户?”不是相同的方法。@user2225129此错误参数数错误(1代表0)这意味着该方法不接受任何参数,而您给它提供的参数从外观上看是@user
我已经改变了这一点,但现在我得到了这个错误=>对于nil:nilclass,未定义的方法“following?”您这里的问题是,当您得到这个错误时,following变量很可能是空的。你到底在哪里设置了下面的,但仍然无法让“当前用户”(@user)”工作!太令人沮丧了!若我使用上面给出的代码,它们是这个视图响应的控制器的错误?关系\u控制器和用户\u控制器我已经改变了这一点,但现在我得到了这个错误=>nil:nilclass的未定义方法'following'。你们这里的问题是,当你们得到这个错误时,following变量很可能是空的。你到底在哪里设置了下面的,但仍然无法让“当前用户”(@user)”工作!太令人沮丧了!若我使用上面给出的代码,那个么它们就是这个视图响应的控制器的错误?关系\控制器和用户\控制器