2015-02-10 50 views
0

我已經通過了一些類似的問題,但仍然無法發現我的行動中的多個重定向。你能幫我發現嗎?還有避免所謂的雙渲染錯誤的任何通用技巧?Rails:幫助找到多個重定向在這個動作請

的代碼如下:

def st_buy_now_process 
    #create user in system 
    encrypted = BCrypt::Password.create(params[:password]) 
    if (User.where("email = ?", params[:email]).exists?)  
    flash[:alert] = 'User exists. Please try again.'  
    redirect_to "/st_buy_now"   
    elsif (User.create(email: params[:email], encrypted_password: encrypted)) 
    begin 
     customer = Stripe::Customer.create({ 
     :description => params[:email], 
     :card => { 
      :number => params[:cc], 
      :exp_month => params[:exp_1], 
      :exp_year => params[:exp_2], 
      :cvc => params[:cvc], 
      :name => params[:name] 
     }, 
     :email => params['email'] 
     })    
     p="#{params['plan']}_#{params['billing-cycle']}" 
     subscr = customer.subscriptions.create(:plan => p) 
     user = User.where("email = ?", params[:email]).first 
     user.payg = ({:cus_id => customer.id, :subscr_id => subscr.id}).to_json 
     user.plan = p 
     user.save 
    rescue Stripe::CardError => e   
     flash[:alert] = 'Please try again' 
     redirect_to "/st_buy_now" 
    end 
    RestClient.post "https://api:key-nnnn", 
     :from => "<[email protected]>", 
     :to => "#{params[:email]}", 
     :subject => "Welcome to test", 
     :text => "\n\n\n#{File.read("app/views/misc/mail_sign_up.txt")}\n\n\n" 
    cookies['logged_in'] = { 
     :value => '1', 
    } 
    cookies['us_id'] = { 
     :value => params[:email], 
    }  
    flash[:notice] = 'Registration successful.' 
    redirect_to :controller => 'misc', :action => 'create_1' 
    else 
    flash[:alert] = 'Please try again' 
    redirect_to "/st_buy_now" 
    end 
end 

錯誤消息凸顯了倒數第二個重定向 - 在"Registration successful."警報之後的行。

+0

能否請您正確格式化。 – Sontya 2015-02-10 21:25:00

+0

如果您創建了'User'並且存在'Stripe :: CardError'(在'elsif'部分中),則您有兩個重定向。 – ptd 2015-02-10 21:27:48

+0

同意@ptd。你應該在'rescue'部分終止,或者用'if ... elsif ... else'改變順序'begin ... end',並且只使用'flash [:alert] ...'一次。 – PatNowak 2015-02-10 22:03:10

回答

1

當你拯救了一個異常,你的代碼的執行從那裏進行正常繼續,所以你redirect_to "/st_buy_now"rescue塊加上redirect_to :controller => 'misc', :action => 'create_1'你得到的錯誤是你的兩個重定向。

您可能想在rescue區塊中輸入return

1

這個怎麼樣,如果客戶得到了進一步建立和進行重定向,如果發生錯誤redirect_to的「/ st_buy_now」

def st_buy_now_process 
    #create user in system 
    encrypted = BCrypt::Password.create(params[:password]) 
    if (User.where("email = ?", params[:email]).exists?)  
     flash[:alert] = 'User exists. Please try again.'  
     redirect_to "/st_buy_now"   
    elsif (User.create(email: params[:email], encrypted_password: encrypted)) 
     begin 
      customer = Stripe::Customer.create({:description => params[:email],:card => {:number => params[:cc], :exp_month => params[:exp_1],:exp_year => params[:exp_2], :cvc => params[:cvc], :name => params[:name]},:email => params['email'] })    
      p="#{params['plan']}_#{params['billing-cycle']}" 
      subscr = customer.subscriptions.create(:plan => p) 
      user = User.where("email = ?", params[:email]).first 
      user.payg = ({:cus_id => customer.id, :subscr_id => subscr.id}).to_json 
      user.plan = p 
      user.save 
      RestClient.post "https://api:key-nnnn", 
      :from => "<[email protected]>", 
      :to => "#{params[:email]}", 
      :subject => "Welcome to test", 
      :text => "\n\n\n#{File.read("app/views/misc/mail_sign_up.txt")}\n\n\n" 
      cookies['logged_in'] = { 
      :value => '1', 
      } 
      cookies['us_id'] = { 
      :value => params[:email], 
      }  
      flash[:notice] = 'Registration successful.' 
      redirect_to :controller => 'misc', :action => 'create_1' 
     rescue Stripe::CardError => e   
      flash[:alert] = 'Please try again' 
      redirect_to "/st_buy_now" 
     end 
    else 
     flash[:alert] = 'Please try again' 
     redirect_to "/st_buy_now" 
    end 
    end 
+0

是的,這也應該工作。 – smathy 2015-02-10 22:23:07

+0

謝謝。我還沒有到那裏,但這有助於。當我嘗試建議的代碼時,我一直得到請再試一次。這讓我想,但如果我應該將用戶留在數據庫上,如果條帶客戶不是由於錯誤而創建的。我不知道有什麼最佳做法,但那是另一個問題。 – willcom 2015-02-11 20:36:10