[英]rescue_from NoMethodError
Having problems figuring this out. 有问题解决这个问题。
trying to do a 试图做一个
rescue_from NoMethodError, :with => :try_some_options
But its not working. 但它不起作用。
EDITED: For testing I'm doing a simple redirect 编辑:为了测试我正在做一个简单的重定向
def try_some_options
redirect_to root_url
end
EDITED 2: Sample of my controller. 编辑2:我的控制器示例。 Added (exception) as recommended below.
添加(例外),如下所示。
I know the reason I'm getting the error. 我知道我收到错误的原因。 Using Authlogic and authlogic_facebook_connect plugin.
使用Authlogic和authlogic_facebook_connect插件。 When user is created from the facebook plugin the "MyCar" model, which is associated with a user is not created like it normally is created if a user registers locally.
当从facebook插件创建用户时,如果用户在本地注册,则不创建与用户相关联的“MyCar”模型。 Since I do call on the user model and reference the users car throughout different parts of the site, I would like to do something like what you see below and eventually put it in my application_controller.
因为我确实调用了用户模型并在网站的不同部分引用了用户汽车,所以我想做一些类似于你在下面看到的内容并最终将它放在我的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
EDITED 3: Hacked it for now since I know why the error is showing up, but would like to figure out how to rescue_from NoMethodError 编辑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
I just read your post when trying to come up with a solution to the same problem. 我只是在试图找出解决同一问题的方法时阅读了你的帖子。 In the end I did the following:
最后我做了以下几点:
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
This worked well for me. 这对我很有用。 It is a catch all situation yet it just may help you.
这是一个捕捉所有情况,但它可能会帮助你。 All the best.
祝一切顺利。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.