Kan*_*ane 11 ruby ruby-on-rails
我在rails app中有这样的方法:
def current_user
return @current_user if @current_user.present?
@current_user = current_user_session && current_user_session.record
end
Run Code Online (Sandbox Code Playgroud)
我之前没有使用过这个.present?方法所以我进入了我的交互式ruby shell来玩它.
每当我使用xxx.present?它时,都会返回NoMethodError.无论xxx是字符串,数字,数组都无关紧要.
我该如何使用这种方法?
Ner*_*rve 22
present是一种方法ActiveSupport.它不是核心红宝石的一部分.这就是为什么你不能在红宝石外壳中使用它.要使用它,您需要在控制台中需要相关的库(例如irb/pry):
require 'active_support'
require 'active_support/core_ext'
Run Code Online (Sandbox Code Playgroud)
这样,您就可以访问所提供的所有实用程序方法activesupport.