gpt4 book ai didi

ruby-on-rails - Rails 从控制台调用 Controller 操作

转载 作者:行者123 更新时间:2023-12-04 03:37:23 25 4
gpt4 key购买 nike

我有一个可以创建 session 的 Controller session 。
我想从控制台调用它,比如 controller.create。
这是操作:



def create
#raise request.env["omniauth.auth"].to_yaml

auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
user.create_or_update_profile(auth)
session[:user_id] = user.id

if user.needs_to_create_profile?
redirect_to new_profile_path, :notice => "Signed in!. We just need your contact e-mail"
else
redirect_to root_url, :notice => "Signed in!"
end
end

最佳答案

从控制台:


include ActionController::TestProcess
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller = SomeController.new

@request.env["omniauth.auth"] = {'provider' => "twitter", 'uid' => "1234", 'user_info' => {'name' => "foo"}}

app.get "/signup" etc


来自 rspec:


it "should allow login" do
request.env["omniauth.auth"] = {'provider' => "twitter", 'uid' => "1234", 'user_info' => {'name' => "foo"}}
post :create
puts @current_user.name
assigns(@current_user).should_not be_nil
end

关于ruby-on-rails - Rails 从控制台调用 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4400888/

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