0

I've seen many posts about rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found, along with the companion method:

def record_not_found
  redirect_to action: :show
end 

This is my first time implementing this, so I'm wondering why I keep getting This webpage has a redirect loop or that my params are incorrect for that route (within the record_not_found method).

When a guest user time reaches a preset timeout limit, the user record will have the #destroy method called on it--even if the guest is still within the app. The guest user has not yet inputted email/password, and the guest column in the users table is set to true. (I actually want a guest to be able to Register w/ email, etc from within the app, transferring whatever data/dependencies the guest user object had acquired to the newly created permanent user.)

I've been putting them in my individual controllers, which I know is not DRY...I'd tried this in ApplicationController and kept getting the This webpage has a redirect loop, but I'm also now getting it in my individual controllers, so I guess that wasn't the problem.

Here is an example from my lessons_controller.rb...

class LessonsController < ApplicationController

  before_action :require_user, :only => [:show]

  rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

  def show
    @lesson = Lesson.find(params[:id])
    @assessment = @lesson.provide_assessment_object
    if @assessment
      @assessment.make_sure_choices_are_instantiated(current_user)
    end
  end

  private

    def record_not_found
      redirect_to controller: :lessons, action: :show, id: params[:id]
    end 

end

My great preference would be to have all the record_not_found methods (hopefully eventually all DRYed up within ApplicationController) to redirect_to the front page which doesn't require a user session.

Any help on this would be most appreciate! Thanks a lot!

Matt
  • 811
  • 2
  • 11
  • 20
  • 1
    So, you try to find a lesson x, find throws an error basically saying that the lesson you searching for does not exists and in `record_not_found ` you redirect to the same lesson x and you get a redirect loop. – cristian Mar 21 '15 at 18:53
  • What is your question? As @cristian stated, the redirect loop is caused by rescuing the exception and then calling the same code that raises the same exception, thus looping forever. – messanjah Mar 21 '15 at 19:13
  • Exactly what cristian said. You've defined an action for `ActiveRecord::RecordNotFound` that redirects to the `show` action of what could not be found. The show action in turn is going to look for for a certain `app` in this case. But it can't find it so it raises the `ActiveRecord::RecordNotFound` exception again looping the entire application. It is considered bad practice to render `404` page when this occurs. Have a look at http://stackoverflow.com/questions/2385799/how-to-redirect-to-a-404-in-rails – Jens Mar 21 '15 at 19:42
  • I looked at http://stackoverflow.com/questions/2385799/how-to-redirect-to-a-404-in-rails and tried this in `ApplicationController`, but now I get the `NameError ` `uninitialized constant ActionController::RecordNotFound` It seems like this is a step in the right direction though. Thanks! – Matt Mar 22 '15 at 01:34

0 Answers0