Beginner Ruby on Rails What I learned is being summarized

Purpose of this article I have summarized what I learned in about 3 months at a programming school so that I can make use of it in my career change activities. This is an article by me only for me. The volume will increase little by little.

One word that left an impression on the mentor was ~ There is a difference in level to be able to write Rails. ~ ~ RSpec is an essential technology. ~

Code review perspective He seems to be a great person for mentors! Here's a summary of the points to check when doing a Rails code review


gem'haml-rails' required. Haml eliminates the need for closing tags and reduces code. Severe indentation, but you'll be able to write neat code without the need for closing tags! I feel. Reference: How to write Haml


<div class="haml">
  <h1>Hello Haml!</h1>

  %h1 Hello Haml!

About routing

Pass the meaning of the URL Nesting resources makes it easier to understand the meaning of URLs. ↓


#Post is the parent, and Comment and Like are the children.
  resources :posts do
    resources :comments, only: [:create, :destroy]
    resources :likes, only: [:show,:create, :destroy]

Moreover, you can bring two IDs from the URL! ↓

post_comments POST     /posts/:post_id/comments(.:format)      comments#create
post_comment  DELETE   /posts/:post_id/comments/:id(.:format)  comments#destroy
post_likes    POST     /posts/:post_id/likes(.:format)         likes#create
post_like     GET      /posts/:post_id/likes/:id(.:format)     likes#show
              DELETE   /posts/:post_id/likes/:id(.:format)     likes#destroy
#It's hard to understand, so let's make it even more


Model method

Make the logic used many times a model method, make it DRY ↓


-  - if == post.user_id #I had a bad face when I wrote it directly
+  - post.created_user?(current_user) 
    %p you are the person who posted


#Define model method
  def created_user?(user)
    self.user_id ==



In the controller, calling a method directly from the model can easily lead to a security hole. Basically, the whole target is irregular, and it is almost nonexistent. ↓


def new
  - @post =  #Wrong ID may be entered
  + @post =  #current_range of user(scope)
#If you really want to use the capital letter model, you have to do it or put your hand on your chest before deciding.


Make the common part in the controller a private method and keep it in DRY with before_action ↓


before_action :set_post


def set_post
  @post = current_user.posts.find(params[:id])
#Don't make the controller fat, let's make it slim

Save, Destroy method

For save and destroy methods that almost never fail except for factors such as server troubles in the Internet environment! ↓


  def destroy
    flash[:notice] = "Deleted post"
    redirect_to posts_path

Use constants

Make it a constant so that it is easy for the whole person to understand. Define a constant in the model and call it in the controller. ↓



@comments =[:page]).per(Ex_model::PER_COMMENT).order(created_at: :desc)
#Model name::Constant name


Originally write a test and write a method while checking for failures (TDD, test-driven development)


  describe "#created_user?" do
    let(:user) { FactoryBot.create(:user) } #The person
    let(:other_user) { FactoryBot.create(:user) } #others
    let(:post) { FactoryBot.create(:post, user: user) } #Posted by the person
    context "For the same user as the logged-in user" do
      it "Returning true" do
        expect(post.created_user?(user)).to eq true 
    context "If not the same user as the logged-in user" do
      it "Returning false" do
        expect(post.created_user?(other_user)).to eq false 

Reference book Everyday Rails-Introduction to Rails Testing with RSpec Anyway, I can move my hand a little. Personally, the ajax related test of system spec is trauma.

Recommended videos TDD Boot Camp 2020 Online # 1 Keynote / Live Coding

Continue ...

Recommended Posts

Beginner Ruby on Rails What I learned is being summarized
Ruby on Rails Overview (Beginner Summary)
What I learned from studying Rails
I summarized the flow until implementing simple_calendar in Ruby on Rails.
What i learned
I made a portfolio with Ruby On Rails
What I learned ② ~ Mock ~
What I learned ① ~ DJUnit ~
Ruby on Rails basics
Ruby On Rails Association
[Ruby] What is true?
I introduced Docker to Rails 6, so I summarized it (beginner)
[Ruby on Rails] I get a warning when running RSpec because gem'chromedriver-helper' is deprecated.
What I was interested in in Progate's Ruby on Rails course [Each statement of error message]
I tried installing Ruby on Rails related plugin with vim-plug
Ruby on rails learning record -2020.10.03
What is a Ruby module?
What is Rails gem devise?
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
What I was addicted to while using rspec on rails
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
I tried Rails beginner [Chapter 1]
[Ruby] What is `!!` used for?
Commentary on partial! --Ruby on Rails
[Ruby] What is an instance?
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
I tried Rails beginner [Chapter 2]
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
What I learned about Kotlin
What is Rails Active Record?
Since I switched from Spring Boot (Java) to Ruby on Rails, I summarized my favorite points of Rails.
Where I was interested in Progate's Ruby on Rails course [params]
I want to add a browsing function with ruby on rails
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
[Ruby on Rails] Introduced paging function
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
What I learned with Java Gold
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
What I learned with Java Silver
What is a Ruby 2D array?
[Ruby on Rails] Stop "looping until ..."