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

Haml

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

example.html.haml


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

.haml
  %h1 Hello Haml!

About routing

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

routes.rb



#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]
  end

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

Model method

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

example.html.haml



-  - if current_user.id == 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

post.rb


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

Controller

Scope

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. ↓

example_controller.rb



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

Before_Action

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

example_controller.rb



before_action :set_post

#abridgement
private

def set_post
  @post = current_user.posts.find(params[:id])
end
#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! ↓

example_controller.rb



  def destroy
    @post.destroy!
    flash[:notice] = "Deleted post"
    redirect_to posts_path
  end

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. ↓

example_controller.rb



#ex_model.rb
PER_COMMENT = 5

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

RSpec

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

post_spec.rb



  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 
      end
    end
    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 
      end
    end 
  end

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 ..."