Good afternoon all,
I’m trying to restrict the access to the application to paths like “localhost:3000/users/1” without a previus login. To do that I have create this function:
‘session_controller.rb’before_action :authorize
def authorize
if current_user.nil?
redirect_to home_path
else
redirect_to user_path(current_user.id)
end
end
When the ‘current_user’ is nil it entryes to an infinite loop. What I have to do to solve this?.
Without actually seeing the code for what is answering home_path, I'm
going to make a guess that that controller+action is making a call to
the session controller.
Yes, when the ‘current_user’ is nil ‘home_path’ calls a method from session controller. I understand the reason of the loop, but I don´t know how to fix it
I need this way because when the user is not loged he can’t access anything from the application.
Remove one or the other, your choice. If your application cannot
handle anonymous users (which is legit thing), don't send anonymous
users back to the home controller from the point where they have to
log in. What you should most likely be doing is directing them to a
login screen instead.
Could you please provide where and how the current_user is being defined. Are you using an gem for registration/authentication or this is
manually defined code?
Also please share the content of controller which contains the action ‘home’(for home_path) ?
Good afternoon all,
First of all thanks for the answers.
Lauree, I been trying to use ‘devise’ gem but I’m to junior yet, so I’m using other way to the users login and logout. But thanks for the advise.
I’m following what Colin said. With this line in the controllers: before_action :authorize, only: [:index, :destroy]
Further I have a question. How can I manage ‘strange’ requests that come to my app like: http://localhost:3000/undefinded
I *think* that is because you are running in development mode. I
think that if you run it in a production environment that
public/404.html will be displayed, but I must admit I am not sure
exactly how this works. Perhaps someone more knowledgeable will add
detail or correct me.
I *think* that is because you are running in development mode. I
think that if you run it in a production environment that
public/404.html will be displayed, but I must admit I am not sure
exactly how this works. Perhaps someone more knowledgeable will add
detail or correct me.
You are correct. The development mode shows the "developer-friendly" errors, and the production mode shows the opaque "user-friendly" errors from the static /public/[nnn].html files. Edit those directly to make them appear any way you like. Remember, they will be served from the / root of the server, so any paths to resources need to be relative from that point.
Do you mean how can you run automated tests in the production
environment or do you mean how can you run your app in the production
environment to see whether it functions as described?