简体   繁体   English

Ruby和Rails:设置current_user if语句

[英]Ruby and Rails: setting current_user if statement

I'm in a controller method. 我在控制器方法中。

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

Returns 返回

#<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

So the if statement is false and the p within the if statement is not run...which would lead me to believe that current_user should not get set to the User.find_by_authentication_token result. 因此,if语句为false且if语句中的p没有运行...这会让我相信current_user不应设置为User.find_by_authentication_token结果。 However, clearly, it does. 但是,显然可以。 If I comment out the if statement, then I get 如果我注释掉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">

As expected. 如预期的那样。 I have no idea why anything within that if statement would have any impact on anything since its condition is false. 我不知道为什么if语句中的任何内容都会对任何事物产生影响,因为它的条件是假的。

I've clearly misunderstood something extra basic, and I'd love for someone to clear my vision. 我显然误解了一些额外的基本知识,并且我希望有人能清除我的愿景。

Is current_user a method? current_user是方法吗? I'm going to assume it is. 我将假设是这样。

The first time you ask for current_user , it is a method, and it is returning that result. 第一次请求current_user ,它是一种方法,并且正在返回该结果。 Then, it gets past the if-statement and the interpreter sees that you have defined a LOCAL VARIABLE to current_user (you haven't assigned anything, but you have now overwritten the method, in the local scope, with a local variable of the same name). 然后,它超越了if语句,并且解释器看到您已为current_user定义了LOCAL VARIABLE(您尚未分配任何内容,但现在已在本地范围内使用相同的本地变量覆盖了该方法名称)。 That local variable has no value, it is nil , so you get nil 该局部变量没有值,为nil ,所以得到nil

Do you have a current_user= method? 您有current_user=方法吗? You need to use "self.current_user = {stuff}" if you want to use the method, instead of defining a local variable. 如果要使用该方法,则需要使用“ self.current_user = {stuff}”,而不是定义局部变量。

Example: 例:

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"

The problem is even though the code in the if statement isn't being executed, it's still being parsed, and Ruby is creating a local variable current_user which prevents access to the method. 问题是,即使if语句中的代码没有被执行,仍在解析中,并且Ruby正在创建一个局部变量current_user ,以防止对该方法的访问。 The code doesn't have to run for Ruby to define the variable for you. 无需为Ruby运行代码即可为您定义变量。

This is the same reason why we don't see any errors when we run this code: 这是运行代码时没有看到任何错误的相同原因:

if false
  bar = "foo"
end
bar #=> nil

whereas running this code does generate errors: 而运行此代码确实会产生错误:

baz #=> NameError: undefined local variable or method `baz' for main:Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM