如何redirect_to路径

时间:2017-09-14 21:18:48

标签: ruby-on-rails ruby

我有这个带有rails的类方法Exception错误私有方法。我希望当条件满足时,我应该通过通知重定向到root_path但是,显示错误消息页面

import py4j.protocol  
from py4j.protocol import Py4JJavaError  
from py4j.java_gateway import JavaObject  
from py4j.java_collections import JavaArray, JavaList

from pyspark import RDD, SparkContext  
from pyspark.serializers import PickleSerializer, AutoBatchedSerializer

# your dataframe what you'd estimate
df

# Helper function to convert python object to Java objects
def _to_java_object_rdd(rdd):  
    """ Return a JavaRDD of Object by unpickling
    It will convert each Python object into Java object by Pyrolite, whenever the
    RDD is serialized in batch or not.
    """
    rdd = rdd._reserialize(AutoBatchedSerializer(PickleSerializer()))
    return rdd.ctx._jvm.org.apache.spark.mllib.api.python.SerDe.pythonToJava(rdd._jrdd, True)

# First you have to convert it to an RDD 
JavaObj = _to_java_object_rdd(df.rdd)

# Now we can run the estimator
sc._jvm.org.apache.spark.util.SizeEstimator.estimate(JavaObj)

1 个答案:

答案 0 :(得分:0)

我相信最优雅的方法是为您的错误创建一个特定的类,然后在ApplicationController中添加rescue_from语句。

所以,让我们说这是你的模特。

class Listing < ApplicationRecord
  class NumberOfPhotosError < StandardError; end
  NUMBER_OF_PERMITTED_PHOTOS = 3
  has_many :photos, dependent: :destroy,before_add: :validate_photo_limit

  private

  def validate_photo_limit(photo)
    raise NumberOfPhotosError.new if photos.size >= NUMBER_OF_PERMITTED_PHOTOS
  end
end

然后,假设您有一个继承自ApplicationController的控制器,它应该看起来像这样。当然,如果您不想将其添加到父控制器,也可以在特定控制器中使用rescue_from语句。

class ApplicationController < ActionController::Base
  rescue_from Listing::NumberOfPhotosError do |error|
    redirect_to(root_path, notice: 'Incorrect number of photos!')
  end
end