Why is below code valid ?
#Both actions are in the same Controller
@subject = Subject.new(subject_params)
redirect_to(:action => 'index')
@subject = Subject.find(params['id'])
We do not have
@subject defined twice. It’s being assigned to different values, in different methods.
That said, when the method
#delete is being called,
@subject takes a value of
Subject.find(params['id']) and when
#new is called, it becomes
I am unsure why it looks suspicious to you. Methods
#delete in Rails are rarely called subsequently, but even if they were, in non pure functional languages it’s totally legit to reassign variables values.