简体   繁体   English

在 rails 控制台中访问 c​​urrent_user

[英]accessing current_user in rails console

I need to access the current_user attribute in rails console, I have the pry gem installed, but I do not know how to use it to sign in a user我需要访问 rails 控制台中的 current_user 属性,我安装了 pry gem,但我不知道如何使用它来登录用户

I also tried我也试过

user = User.find(1)
sign_in user

in my code to manually log a user in, but this returns the error在我的代码中手动登录用户,但这会返回错误

Module::DelegationError: ActionController::Metal#session delegated to @_request.session, but @_request is nil: #<ResumesController:0x0055be48209b10 @_action_has_layout=true, @_routes=nil, @_request=nil, @_response=nil>

The whole concept of a "current user" is not applicable when using the console.使用控制台时,“当前用户”的整个概念不适用。

The console unlike when you run a rails server is not actually responding to http requests.与运行 rails 服务器时不同的控制台实际上并未响应 http 请求。 It just boots up the key railties of the framework so that you can play around with it.它只是启动框架的关键轨道,以便您可以使用它。

Since there is no HTTP request, there is no session or token based auth.由于没有 HTTP 请求,因此没有基于会话或令牌的身份验证。 Thus there is no user actually interacting with the system which renders the whole concept moot.因此,没有用户实际与系统交互,这使得整个概念没有实际意义。

Pry

The pry gem is an alternative console to the default ruby irb . pry gem 是默认 ruby irb的替代控制台。

It can be used to set "interactive breakpoints" in the code so you can halt the execution in the server and inspect variables.它可用于在代码中设置“交互式断点”,以便您可以在服务器中停止执行并检查变量。

So lets say you have:所以让我们说你有:

class PagesController
  # GET /
  def home
    binding.pry
  end
end

When you request http://localhost:3000/ with your browser the page will not load.当您使用浏览器请求http://localhost:3000/ ,页面将不会加载。

If you switch back to console where you are running the rails server you will notice that there is an interactive prompt.如果您切换回运行 rails 服务器的控制台,您会注意到有一个交互式提示。 Once you close the prompt (with exit or ctrl + d) the response will be sent to the browser.一旦关闭提示(使用 exit 或 ctrl + d),响应将发送到浏览器。

The byebug gem is similar but creates an interactive terminal in a standard rails error page. byebug gem 类似,但在标准 rails 错误页面中创建了一个交互式终端。

when you say "rails console", do you mean running it standalone with "rails console" or do you mean the console shown when you use binding.pry in your code and excecution stops and lets you interact with the current request?当您说“rails 控制台”时,您的意思是使用“rails 控制台”独立运行它,还是指在代码中使用 binding.pry 并停止执行并让您与当前请求交互时显示的控制台?

you can't set a current_user from a standalone console like @max said because it's a concept related to requests and sessions你不能像@max 所说的那样从一个独立的控制台设置 current_user 因为它是一个与请求和会话相关的概念

you should be able to log in a user if you are talking about the console that stops the current request excecution usingin binding.pry, I'm guessing you are using devise and that code to sign_in would be ok, only that you are mixing the two contexts如果您正在谈论使用 binding.pry 停止当前请求执行的控制台,您应该能够登录用户,我猜您正在使用设计并且用于 sign_in 的代码可以,只是您正在混合两种语境

as long as you do not need session behavior, just a "current user" eg like this只要您不需要会话行为,只需一个“当前用户”,例如像这样

def load_something_for( user = User.current ) . . .

this should work:这应该有效:

[4] pry(main)> User.current = (User.find_by login: :mr_x )
[5] pry(main)> load_something_for() # for Mr.X

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

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