The key point is: the same current flow through both resistors $R_1,R_2$.

For non-invert case:
$V_{out}=I\,(R_1+R_2), \qquad V_{in}=I\, R_1$
so $V_{out}=\frac{R_1+R_2}{R_1} V_{in}=(1+\frac{R_2}{R_1}) V_{in}$

For invert case:

$V_{out}= -I\, R_2, \qquad V_{in}=I\, R_1$
so $V_{out}=-\frac{R_2}{R_1}\, V_{in}$