Rails:请帮助发现此操作中的多重重定向

时间:2015-02-10 21:22:38

标签: ruby-on-rails ruby

我遇到了很多类似的问题,但仍然无法在我的行动中发现多重重定向。你能帮我发现吗?还有任何关于避免所谓的双重渲染错误的通用技巧吗?

代码如下:

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 => "<test@test.com>",
      :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."警报之后的行。

2 个答案:

答案 0 :(得分:1)

当您拯救例外时,正常情况下会继续执行您的代码,因此redirect_to "/st_buy_now"阻止中的rescue加上redirect_to :controller => 'misc', :action => 'create_1'您的错误是你的两个重定向。

您可能希望在return块中抛出rescue

答案 1 :(得分:1)

如果这样,如果客户被创建继续进行并重定向并且如果发生错误redirect_to&#34; / st_buy_now&#34;

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 => "<test@test.com>",
            :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