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
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.
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.
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
Ruby on Rails Guide https://railsguides.jp/getting_started.html
Recommended Posts