rescue_from NoMethodError

有问题搞清楚这一点。

试图做一个

rescue_from NoMethodError, :with => :try_some_options 

但它不起作用。

编辑:为了测试我正在做一个简单的重定向

 def try_some_options redirect_to root_url end 

编辑2:我的控制器示例。 添加(例外),如下所示。

我知道我收到错误的原因。 使用Authlogic和authlogic_facebook_connect插件。 当从facebook插件创建用户时,如果用户在本地注册,则不会创建与用户相关联的“MyCar”模型。 由于我确实调用了用户模型并在网站的不同部分引用了用户汽车,因此我想做一些类似于你在下面看到的内容并最终将它放在我的application_controller中。

 class UsersController  [:new, :create] rescue_from NoMethodError, :with => :try_some_options ... def show store_target_location @user = current_user end def create @user = User.new(params[:user]) if @user.save MyCar.create!(:user => @user) flash[:notice] = "Successfully created profile." redirect_to profile_path else render :action => 'new' end end ... protected def try_some_options(exception) if logged_in? && current_user.my_car.blank? MyCar.create!(:user => current_user) redirect_to_target_or_default profile_path end end ... end 

编辑3:暂时将其破解,因为我知道错误出现的原因,但是想弄清楚如何使用rescue_from NoMethodError

 class UsersController  [:new, :create] before_filter :add_car_if_missing def add_car_if_missing if logged_in? && current_user.my_car.blank? MyCar.create!(:user => current_user) end end end 

我只是在尝试找出解决同一问题的方法时阅读了您的post。 最后我做了以下事情:

 class ExampleController < ApplicationController rescue_from Exception, :with => :render_404 ... private def render_404(exception = nil) logger.info "Exception, redirecting: #{exception.message}" if exception render(:action => :index) end end 

这对我很有用。 这是一个捕捉所有情况,但它可能会帮助你。 祝一切顺利。