Since I switched from Spring Boot (Java) to Ruby on Rails, I summarized my favorite points of Rails.

Spring Boot was a good guy, but Ruby on Rails is my favorite!

I started programming from Java. The purpose was to make use of it in my work, Right now, I mainly make web services as a hobby. I was originally interested because I heard that Ruby on Rails is the mainstream in personal development and ventures. However, even though Java is also amateurish, learning another language is too expensive, and I thought it would be good if it could be shaped as a Web service.

So even with the Java framework, I was developing using Spring Boot, which is new and simple and quick to develop. From development to release with Spring Boot, I was able to do what I wanted to do. Still, I was curious about Ruby On Rails, but on the other hand, I was also attached to Spring Boot to some extent. I thought it was a little cool to use a framework different from other people, so I planned to continue using it as it is.

This was about 3 weeks ago.

I came up with the web service I wanted to create, and this time I started development with the goal of releasing it in a few weeks. However, I was addicted to Twitter login authentication. Of course, I've been having troubles because it didn't work well, so I've been researching various things, but the information on Twitter authentication using Spring Boot (I want to link Spring Social and Spring Security) is extremely small even if it is included in English. .. I tried none of them and it didn't work. I used it here for 2 days. (About 8 hours) To be honest, there were many parts where I could not understand the process of processing. I think there was also a way to look up the official reference from scratch and implement it on your own. I think it's okay to take that much time to implement the original function, but I had a strong desire to honestly copy and paste general parts such as Twitter authentication. When I looked it up in Ruby on Rails, I found many samples and the implementation was simple.

After that, I would like to research Rails and implement Web services individually with a sense of speed. In response to the request I clearly thought that Ruby On Rails was suitable. Learning costs are high, but if you look at it over a period of several months, it should be more efficient to switch. We decided to stop the service development and switch to it. I also switched from VScode to Atom.

Study history of Ruby on Rails

I studied for about 2 weeks (40-50 hours). Read the Ruby version of dot install and the Ruby on Rails tutorial on the train and during lunch break It's like moving your hands after returning home to implement it.

Ruby on Rails Tutorial was skipped, and it took roughly 3 weeks to read through → implement → read through. From here, I plan to study the parts that I do not understand while actually creating the service.

Spring Boot vs Ruby on Rails Ruby on Rails was the best!

There are overwhelmingly many free documents and samples.

Insanely important. Especially since there is no one who can easily listen as a self-taught scholar The activity of Google teacher becomes a matter of life and death.

Spring Boot: Spring Boot does not have a lot of information on the Web. So I searched for a reference book, but I couldn't tell whether it was good or bad because there were about 4 options and few book reviews. I didn't have a reserve at a bookstore in my neighborhood, so I went to Shinjuku to buy it.

Ruby on Rails: I haven't paid 1 yen for learning this time. Ruby on Rails tutorials, online learning services for beginners It was a very easy-to-follow framework because you can learn a lot by searching. We have abundant samples, so we can expect to speed up future development.

Easy to set up the execution environment

Spring Boot: Java install, pass through, Maven install, pass through, It took about an hour to start using it. Beginners are at a frustrating level.

Ruby on Rails: I use a MAC, but it was so easy that I forgot how I made it. ※Forgot.

Easy to create a project

Spring Boot: Go to Spring Initializer and create a package. I still don't know what to put in the Group and Artiface items, and there are many selection items.

Ruby on Rails: Development can be started quickly with rails new from the console. It seems that the DB type can also be specified as an option.

Excellent folder structure default

Spring Boot: By default, you can't do anything in particular, so you need to decide the placement rules and create the folder yourself. At first, I didn't know the best practice, and I was wondering if this was all right, but I managed to do it with my own MVC.

Source folder made by Pochipochi: スクリーンショット 2018-06-10 19.09.13.png

Ruby on Rails: When you create a package with Rails new, folders for different purposes such as model, view, config, etc. are automatically created. Even if you don't care about the source code layout, it will be organized according to the default, which is helpful for beginners. You can omit the usual simple work of automatically generating it.

Automatically generated folder Source folder: スクリーンショット 2018-06-10 19.07.00.png

Simple dependency package management

Spring Boot: It is managed by pom (Maven). The items described are like this.

pom.xml


 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

Ruby on Rails: It is managed by a gem file. The items described are the same. Is it a little simple?

Gemfile


gem 'rails', '~> 5.2.0'

Easy to understand URL mapping

Spring Boot: The controller handles URL and view mapping and passing values. Since the mapping is described across multiple files, which URL is which view and controller I often lose track of whether it is related. value =" / " is the corresponding URL mav.setViewName ("index ") Find and display the template (HTML file) named index. There are various other definition methods.

indexController.java


@Controller
public class indexContoroller{

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView showIndex (ModelAndView mav){
    mav.setViewName("index");
	return mav;
    }   
}

Ruby on Rails: URL mapping is defined in routers.rb. Only the URL mapping is cut out from the Spring Boot controller. It is highly readable because it can be listed. get'/ help', to:'static_pages # help' has'/ help' as the URL A class with processing to be executed by'static_pages # help'.

routers.rb


Rails.application.routes.draw do
  get  '/help', to: 'static_pages#help'

static_pages_controller.rb


class StaticPagesController < ApplicationController
 
    def help; 
    #hogehoge
    end

ends

Abstract enough to forget the existence of DB

Spring Boot: When you want to use DB, prepare a database, create a table, After that, you need to prepare several classes for DB connection. The operation is abstracted, but the number of steps to be able to write data is not small.

It's a bit long, but it describes the class prepared to operate the Member table. Member.java corresponds to the Member table and receives the data. You can customize the query with MemberRepository.java.

The actual access is @Autowired MemberRepository repository; in the class you want to use Create a repository instance in and use it like repository.findById (id);.

JpaRepository <Member, Integer> will cause an error if you do not specify the type of the table's primary key.

MemberRepository.java


public interface MemberRepository extends JpaRepository<Member, Integer> {
    public Member findByUsername(String username);
    public boolean existsByUsername(String username);
}

Class corresponding to the table:

Member.java


Entity
@Table(name = "member")
public class Member implements UserDetails {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    // private Long userid;
    private int userid;

    @Column(nullable = false, unique = true)
    private String username;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private String displyname;

    public Member() {
        super();

    }

    public String getDisplyname() {
        return displyname;
    }

    public int getUserid() {
        return userid;
    }

    public void setDisplyname(String displyname) {
        this.displyname = displyname;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    public String getMail() {
        return this.displyname;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}

Ruby on Rails: rails generate model" table name "content: text When you execute the command like, the table and the class required for the operation are automatically generated. You can suddenly read and write from the DB as shown below.

m = "table name".new
m.find_by()`

I have never connected directly to the DB so far. Rails almost forgets the existence of DB.

Gem is excellent

Spring Boot: I implemented the image upload function in Spring Boot, but it was quite difficult. Upload and validate in HTML and JS, After receiving it, put the path of the file in the DB and save the file locally. It was necessary to write the process from scratch.

Ruby on Rails: Regarding image upload, there is a gem called CarrierWave, It can be implemented with about 1/3 of the processing of Spring Boot. Spring has a lot of useful features, but Rails is better. I have a good impression when it comes to web development.

Web server bundle

Spring Boot: In fact, Spring Boot is also bundled with Tomcat, so this is a draw. If it is the default port 80, it cannot be started locally on the MAC, so it is necessary to specify the port number and start it.

Ruby on Rails: The port number 3000 seems to be the default, so you don't have to do anything.

Others Ruby on Rails good point

--Easy validation after receiving data --Simple template engine (Eruby vs Thymelefe) --Easy to componentize HTML --Easy to read JS and CSS (asset pipeline) --The flow from form to data writing is beautiful --Ajax Easy. JS Almost no need to write --The value is automatically passed from the controller to the view. --A view is automatically created when you create a controller

Summary

Rails has a lot of troublesome work in the background, I'm writing a program, but it feels like I'm playing a puzzle or a game. As for Spring Boot, the function can be implemented after going through various procedures, so the team was solid. I think that it is suitable for making large applications, but at the personal development level The current conclusion is that you don't need that tightness.

Recommended Posts

Since I switched from Spring Boot (Java) to Ruby on Rails, I summarized my favorite points of Rails.
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
Summary of points I was worried about when migrating from java to kotlin
05. I tried to stub the source of Spring Boot
[Updated from time to time] Ruby on Rails Convenient methods
I tried to reduce the capacity of Spring Boot
From Java to Ruby !!
[Ruby on Rails] From MySQL construction to database change
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
[Ruby on Rails] Since I finished all Rails Tutorial, I tried to implement an additional "stock function"
The story of raising Spring Boot from 1.5 series to 2.1 series part2
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
From Ruby on Rails error message display to Japanese localization
Beginner Ruby on Rails What I learned is being summarized
Try Spring Boot from 0 to 100.
Ruby on Rails --From environment construction to simple application development on WSL2
I want to add a browsing function with ruby on rails
I just want to write Java using Eclipse on my Mac
02. I made an API to connect to MySQL (MyBatis) from Spring Boot
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
I want to control the default error message of Spring Boot
I summarized the flow until implementing simple_calendar in Ruby on Rails.
Basic knowledge of Ruby on Rails
How to use Ruby on Rails
Upgrade spring boot from 1.5 series to 2.0 series
[Java] Flow from introduction of STS to confirmation of dynamic page on localhost (2/3)
[Spring Boot] I investigated how to implement post-processing of the received request.
I want to display images with REST Controller of Java and Spring!
[Java] Flow from introduction of STS to confirmation of dynamic page on localhost (1/3)
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
How to solve the local environment construction of Ruby on Rails (MAC)!
[Java Spring MVC] I want to use DI in my own class
I translated the grammar of R and Java [Updated from time to time]
[Ruby On Rails] How to search the contents of params using include?
From pulling docker-image of rails to launching
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] How to use CarrierWave
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
I can't install rails on my mac
Story when moving from Spring Boot 1.5 to 2.1
Deploy to Heroku [Ruby on Rails] Beginner
[day: 5] I summarized the basics of Java
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Preparing to introduce jQuery to Ruby on Rails
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
[Ruby on Rails] Japanese notation of errors
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
I tried to build Ruby 3.0.0 from source
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
[Ruby on Rails] Button to return to top
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
[Ruby on Rails] Change the update date and creation date to your favorite notation
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed