gpt4 book ai didi

ruby-on-rails - ruby 和 Rails : setting current_user if statement

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:44 24 4
gpt4 key购买 nike

我在 Controller 方法中。

p current_user
p request.referrer != Rails.application.routes.url_helpers.dashboard_patients_add_url
if request.referrer != Rails.application.routes.url_helpers.dashboard_patients_add_url
p 'in here'
current_user = User.find_by_authentication_token(params[:token])[0]
end
p current_user

返回

#<User id: 33, first_name: "1", last_name: "1", working_at: "1", password_digest: nil, password_confirmation: nil, created_at: "2014-03-20 00:37:54", updated_at: "2014-03-20 00:38:00", email: "aab@gmail.com", encrypted_password: "$2a$10$nw1UHb0PeFUFh17zKRhbsOA.MirNfXW69wj7aRfMSDEw...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-03-20 00:37:54", last_sign_in_at: "2014-03-20 00:37:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", authentication_token: "HHz6KyqrHF7pC41DyGKH", salutation: "1", speciality: "1", cell_phone_number: "1", office_phone_number: "1">
false
nil

因此 if 语句为 false,并且 if 语句中的 p 未运行...这让我相信 current_user 不应设置为 User.find_by_authentication_token 结果。然而,很明显,它确实如此。如果我注释掉 if 语句,那么我得到

#<User id: 33, first_name: "1", last_name: "1", working_at: "1", password_digest: nil, password_confirmation: nil, created_at: "2014-03-20 00:37:54", updated_at: "2014-03-20 00:38:00", email: "aab@gmail.com", encrypted_password: "$2a$10$nw1UHb0PeFUFh17zKRhbsOA.MirNfXW69wj7aRfMSDEw...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-03-20 00:37:54", last_sign_in_at: "2014-03-20 00:37:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", authentication_token: "HHz6KyqrHF7pC41DyGKH", salutation: "1", speciality: "1", cell_phone_number: "1", office_phone_number: "1">
false
#<User id: 33, first_name: "1", last_name: "1", working_at: "1", password_digest: nil, password_confirmation: nil, created_at: "2014-03-20 00:37:54", updated_at: "2014-03-20 00:38:00", email: "aab@gmail.com", encrypted_password: "$2a$10$nw1UHb0PeFUFh17zKRhbsOA.MirNfXW69wj7aRfMSDEw...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-03-20 00:37:54", last_sign_in_at: "2014-03-20 00:37:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", authentication_token: "HHz6KyqrHF7pC41DyGKH", salutation: "1", speciality: "1", cell_phone_number: "1", office_phone_number: "1">

正如预期的那样。我不知道为什么 if 语句中的任何内容都会对任何内容产生任何影响,因为它的条件是错误的。

我显然误解了一些更基本的东西,我希望有人能澄清我的看法。

最佳答案

current_user 是一个方法吗?我假设它是。

第一次请求 current_user 时,它是一个方法,它返回那个结果。然后,它通过 if 语句,解释器看到您已经为 current_user 定义了一个局部变量(您没有分配任何东西,但是您现在已经在局部范围内覆盖了该方法,具有相同名称的局部变量)。那个局部变量没有值,它是 nil,所以你得到 nil

你有 current_user= 方法吗?如果要使用该方法,则需要使用“self.current_user = {stuff}”,而不是定义局部变量。

例子:

class MyClass
def current_user
@current_user
end
def initialize(u)
@current_user = u
end
def current_user=(u)
@current_user = u
end
def do_things_bad
p "current user is #{current_user}"
if false
current_user = "something else"
end
p "current user after if is #{current_user}"
end
def do_things_good
p "current user is #{current_user}"
if false
self.current_user = "something else"
end
p "current user after if is #{current_user}"
end
end
thing = MyClass.new("Nathan")
#<MyClass:0x00000100c5def0 @current_user="Nathan">
thing.do_things_bad
#=> "current user is Nathan"
#=> "current user after if is "
thing.do_things_good
#=> "current user is Nathan"
#=> "current user after if is Nathan"

关于ruby-on-rails - ruby 和 Rails : setting current_user if statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22545045/

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