簡體   English   中英

Rails從控制台調用控制器操作

[英]Rails calling a controller action from the console

我有一個可以創建會話的控制器會話。 我想從控制台調用它,比如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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM