Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Private Methods?

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?

like image 329
hellomello Avatar asked May 12 '13 17:05

hellomello


2 Answers

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?

like image 137
MrTheWalrus Avatar answered Oct 03 '22 09:10

MrTheWalrus


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.

like image 38
Raindal Avatar answered Oct 03 '22 08:10

Raindal