If I'm writing a private method, does rails think that every method under the word private
is going to be private? or is it supposed to be only private for the first method?
private
def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
does that mean signed_in_user
and correct_user
is private? or just signed_in_user
? Does that mean whenever I need to write private methods, it should be at the end of my file now?
Yes, each method after the private
keyword will be private. If you want to change back to defining non-private methods, you can use a different keyword, like public
or protected
.
See Where to place private methods in Ruby?
Yes all the methods under private
are private. Usually you will, indeed, find those methods at the bottom of your file.
But you can "stop" this by writing another keyword like protected
and then all the methods following would be protected.
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