Rails' Private methods hurt if you feel the same as Java!

Introduction

Ruby on Rails private method

I created a controller to create or reference the article below, but @articles got an error in Nil when opening the article details (I was using @articles in HTML and I got an error there T) It was completely unexpected for me

articles_controller.rb


def index
  @articles = Article.paginate(page: params[:page], per_page: 5)
end
  
private
  def article_params
    params.require(:article).permit(:title, :description)
  end
  
def new
  //abridgement
end
    
def create
  //abridgement
end
    
def show
end
Until resolution

Simple, but solved by looking at the reference

The standard CRUD actions for each controller are often arranged in the order index, show, new, edit, create, update, destroy. It doesn't have to be in this order, but keep in mind that they are all public methods. As already mentioned in this guide, the controller's public methods must be placed before private.

It was listed to the fullest ... The error was resolved by rewriting the private method after the public method If you define private, it seems that it will be a private method after that unless you specify public etc.

Cause

Well, it's bad that I didn't look at the reference properly, but I also wondered if it was because I was proceeding with the same feeling as Java.

test.java


public void index() {
    //abridgement
}

private void set_article() {
    //abridgement
}

public void show() {
    //abridgement
}  

In Java, the above implementation does not generate an error, and set_article () and later are not private methods. That's right, because each method defines a modifier.

Summary

I learned two things from this time ① Look at the reference when learning a new language! (Search anyway) ② When learning a new language, forget the conventional stereotypes once, and try to find similar and different parts as you study.

Also, try & error is important

reference

Ruby on Rails Guide https://railsguides.jp/getting_started.html

Recommended Posts

Rails' Private methods hurt if you feel the same as Java!
If you want to satisfy the test coverage of private methods in JUnit
Ruby's fine syntax: if you have variables and methods with the same name
Java desktop with the same front end as the WEB.
Implement the same function as C, C ++ system ("cls"); in Java
What to do if you can't use the rails command
Check Java9 Interface private methods
[PostgreSQL] If you want to delete the Rails app, delete the database first!
If you want to change the Java development environment from Eclipse
Determine if the strings to be compared are the same in Java
[Java] Test private methods with JUnit