Rescue_from NoMethodError

You are having trouble with this.

trying to do

rescue_from NoMethodError, :with => :try_some_options 

But it does not work.

Editorial: For testing, I am doing a simple redirect.

 def try_some_options redirect_to root_url end 

IMAGE. 2: Sample of my controller. Added (exception), as recommended below.

I know the reason why I get the error. Using the Authlogic module and authlogic_facebook_connect. When a user is created from the facebook plugin, the MyCar model associated with the user is not created, as is usually created if the user is registered locally. Since I call the user model and reference the user car in different parts of the site, I would like to do something like what you see below, and ultimately put it in my 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 

EDIT 3: hacked it now, since I know why the error appears, but I would like to find out how 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 
+4
source share
1 answer

I just read your post trying to find a solution to the same problem. I ended up doing 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 

It worked for me. This is another problem, but it can help you. All the best.

+6
source

All Articles