簡體   English   中英

Ruby和Rails:設置current_user if語句

[英]Ruby and Rails: setting current_user if statement

我在控制器方法中。

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定義了LOCAL VARIABLE(您尚未分配任何內容,但現在已在本地范圍內使用相同的本地變量覆蓋了該方法名稱)。 該局部變量沒有值,為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"

問題是,即使if語句中的代碼沒有被執行,仍在解析中,並且Ruby正在創建一個局部變量current_user ,以防止對該方法的訪問。 無需為Ruby運行代碼即可為您定義變量。

這是運行代碼時沒有看到任何錯誤的相同原因:

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

而運行此代碼確實會產生錯誤:

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