Search code examples
ruby-on-rails-4routesrails-routingrescue

Should Show 404 error when user provide invalid url in rails


Suppose my website URL is http://localhost:3000, But when user manually type http://localhost:3000/orders, he should be redirected to root_path or some other path because this url is only valid after session is created. and I am getting
NoMethodError in OrdersController#index

undefined method `orders' for nil:NilClass enter image description here


Solution

  • To make sure user will be redirected to root url you have to use some before_action (before_filter in Rails 3) to your controller.

    For example if you're using Device gem for authentication you have to add to your controller:

    before_action :authenticate_user! (more details...)

    If you have your own authentication system you have to implement manually something like device's authenticate_user! method to set current_user.

    If I correctly understood your issue you should get the idea.