Rails 从控制台调用控制器操作 [英] Rails calling a controller action from the console

查看:28
本文介绍了Rails 从控制台调用控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a controller sessions that can create session. I'd like to call it from the console, like controller.create. Here is the action:



  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

这篇关于Rails 从控制台调用控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆