Trying to figure out why my nil check fails when calling a method with no param, or a param id which yields no records.
@game = Game.where(:id => params[:id]).first
if @game.nil?
redirect_to root_path
end
In console this works fine.
>> pp @game.nil?
=> true
In application this fails (it never redirects!), why?
In console ( with param id nil or non existent record value): This works:
unless Game.exists?(params[:id])
raise('ok')
end
But not in the real life application :( I tried almost every way to check the record to be existent or valid, the code just runs past this check and continues as is
Looking at some other code I noticed I used a return statement IT seems to solve it with that so..
Works:
unless Game.exists?(params[:id])
redirect_to root_path
return
end
Fails:
unless Game.exists?(params[:id])
redirect_to root_path
end
Not quite sure why it needs an return after the redirect_to explicit
If the redirect_to
instruction is not the last instruction in your controller, the redirect will never happen.
if @game.nil?
redirect_to root_path
return
end
render @game
Without the return, the redirect_to
will be overridden by a render
. You have to see it like this: Rails will not immediately redirect upon a redirect_to instruction. It will set the instruction somewhere and once your Controller returns, it will retrieve if there is a set action to do, if not, it will jump to the default action ("render action view")
Possibly would be nice if there was a warning that you overwrite your action if you have multiple redirect
s/render
s, but other than that, this is totally fine behaviour.
Regards.
EDIT
On a side note, if you're using Rails 4, use Game.find_by(id: params[:id])
instead of Game.where(id: params[:id]).first
.
If you simply want to check existance, Game.exists?(params[:id])
is a nice way, as others have mentioned. Game.find(params[:id])
will throw an error if id
can't be found. A good hint would be to work with slugs as people might guess your games' IDs which is basically a security vulnerability.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With