在解决这个问题上有问题。
试着做一个
rescue_from NoMethodError, :with => :try_some_options但它不起作用。
编辑:为了测试,我做了一个简单的重定向
def try_some_options
redirect_to root_url
end编辑2:我的控制器样本。已添加(例外),如下所示。
我知道我得到这个错误的原因。使用授权逻辑和authlogic_facebook_connect插件。从facebook插件创建用户时,与用户关联的"MyCar“模型不会像通常在用户本地注册时那样创建。由于我确实在站点的不同部分调用了用户模型并引用了用户的汽车,因此我想做一些如下所示的操作,并最终将其放入我的application_controller中。
class UsersController < ApplicationController
before_filter :login_required, :except => [: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 < ApplicationController
before_filter :login_required, :except => [: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发布于 2011-05-19 15:33:13
我刚刚读了你的帖子,试图想出一个解决同样问题的方法。最后,我做了以下工作:
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这对我来说效果很好。这是一个包罗万象的情况,但它可能会对你有所帮助。万事如意。
https://stackoverflow.com/questions/3632224
复制相似问题