create方法中有多个if语句

时间:2017-07-13 14:45:43

标签: ruby-on-rails ruby ruby-on-rails-4

我希望在创建操作中传递两个方法@result@subscription_result。我首先要成功传递@result,然后再传递@subscription_result

如何纠正以下创建操作才能使其工作?

def create

    @result =.....

     if @result.success?
      puts @result.customer.id
      redirect_to root_path, notice: 'success'

    @subscription_result = .......

    if @subscription_result.success? 
      puts @subscription_result.susbcription.id

    else
      redirect_back( fallback_location: (request.referer || root_path),
                 notice: "Please try again!")
    end
  end

1 个答案:

答案 0 :(得分:1)

如果我理解正确,这是一个最简单的愚蠢解决方案:

def create
  @result =.....
  @subscription_result = .......

   if @result.success? && @subscription_result.success?
    puts @result.customer.id
    puts @subscription_result.susbcription.id
    redirect_to root_path, notice: 'success'
  else
    redirect_back( fallback_location: (request.referer || root_path),
               notice: "Please try again!")
  end
end

或类似的东西:

after_action :subscription_result, only: [:create]

def create
  @result =.....

   if @result.success? 
    puts @result.customer.id
    redirect_to root_path, notice: 'success'
  else
    redirect_back( fallback_location: (request.referer || root_path),
               notice: "Please try again!")
  end
end

private

def subscription_result
  @subscription_result = .......
  puts @subscription_result.susbcription.id if @subscription_result.success?
end

希望它会对你有所帮助!