Ruby on Rails undefined local variable or method

1k views Asked by At

I'm having a problem with redirecting my website to a different page, if the person is younger then a certain age. Either, I need more coffee or I've lost it, I can't seem to see what I did wrong. Thanks so much.

app/models/student.rb

Im trying to sent people to a different site with redirect

 private
  def must_be_over_13
    if birthday && birthday > 13.years.ago
      redirect_to under_13_landing_page_path ``
    end
  end

I defined under_13_landing_page

controller/student controller

 def under_13_landing_page
  end

views/students

I made the redirect page

under_13_landing_page.html

config/routes.rb

I told it to redirect it to the landing page

get "games_for_kids", to: "students#under_13_landing_page", as: 'under_13_landing_page'

THE ERROR**undefined local variable or method `under_13_landing_page_path' for #**

1

There are 1 answers

1
Kirti Thorat On BEST ANSWER

rendering and redirection are Controller's responsibility NOT Model's. You should not be redirecting in the Model app/models/student.rb. This is the reason of the code failure as Model doesn't have access to the route helper methods such as under_13_landing_page_path in your case.

I would suggest you to return a boolean value from Student#must_be_over_13 method which you can then check in the concerned controller and redirect appropriately.