Backend/RubyOnRails

RubyOnRails Getting Start(Blog ๋งŒ๋“ค๊ธฐ) 2ํŽธ

Seyun(Marco) 2021. 2. 16. 19:51
728x90

RubyOnRails Getting Start(Blog ๋งŒ๋“ค๊ธฐ) 2ํŽธ

๐Ÿ’ผ ์„œ๋ก 

  • RubyOnRails Getting Started ์— ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ๊ฒŒ์‹œํŒ CURD ๋ฐ ๋Œ“๊ธ€ ๊ธฐ๋Šฅ๊นŒ์ง€ ๊ฐœ๋ฐœํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค
  • Ruby version์€ 2.6.3์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • Ruby On Rails version์€ 5.2.1์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ„ Article ์ „์ฒด ์กฐํšŒ

  • bin/rails routes ๋กœ api๋ฅผ ์กฐํšŒํ•˜๋ฉด ์ „์ฒด ์กฐํšŒ API๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
Prefix   Verb   URI Pattern         Controller#Action
articles GET    /articles(.:format) articles#index
  • ๊ทธ๋Ÿผ ์ผ๋‹จ ArticlesController์— index ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์—…๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด๋ณด์ž.
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end
end
  • all ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด Article์˜ ์ „์ฒด ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋‘๋ฒˆ์งธ๋กœ ํ• ์ผ์€ view๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด์ฃ ! index.html.erb ๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.
<h1>Listing articles</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th></th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
    </tr>
  <% end %>
</table>

ruby_on_rails_getting_start_blog_2-1

  • acrticle_path ๋Š” ํ—ฌํผ๋กœ article์— ๊ด€๋ จ url์„ ์•Œ๋ ค์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ”— ๋งํฌ ์ถ”๊ฐ€

  • ์ง€๊ธˆ๊นŒ์ง€ ํ–ˆ๋˜ ์ž‘์—…์œผ๋กœ Article ์ž‘์„ฑ ๋ฐ ์กฐํšŒ๋ฅผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ—ˆ์ „ํ–ˆ๋˜ root page๋ฅผ ์ˆ˜์ •ํ•ด๋„๋ก ํ•˜์ฃ .
  • welcome/index.html.erb
<h1>Hello Rails</h1>
<%= link_to 'My Blog', controller: 'articles' %>
<%= link_to 'New article', new_article_path %>
  • ๊ทธ๋ฆฌ๊ณ  ์ „์ฒด ์กฐํšŒ view์—์„œ ์ƒˆ๋กœ์šด ๊ธ€์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งํฌ๋ฅผ ์—ฐ๊ฒฐํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
<h1>Listing articles</h1>

<%= link_to 'New article', new_article_path %>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th></th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
    </tr>
  <% end %>
</table>
  • ์ž‘์„ฑ ์ค‘์— ๋’ค๋กœ ๋Œ์•„๊ฐ€๋Š” link๋„ ์—ฐ๊ฒฐํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
<h1>New Article</h1>

<%= form_with scope: :article, url: articles_path, local: true do |form| %>  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>

<%= link_to 'Back', articles_path %>
  • ํ•˜๋‚˜์˜ ๊ธ€์„ ๋ณด๊ณ  ์žˆ๋‹ค๊ฐ€ ์ „์ฒด ๊ธ€์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งํฌ๋ฅผ ์—ฐ๊ฒฐํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
<h1>New Article</h1>

<%= form_with scope: :article, url: articles_path, local: true do |form| %>  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>

<%= link_to 'Back', articles_path %>

Tip

  • ๋™์ผํ•œ controller๋ฅผ ์—ฐ๊ฒฐํ•  ๊ฒฝ์šฐ์—๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์„ค์ •ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ welcome/index.html.erb ์ด ํŒŒ์ผ์€ ๋‹ค๋ฅธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋–„๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
<%= link_to 'My Blog', controller: 'articles' %>

๐Ÿ‘จโ€โš•๏ธ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

  • ๊ฐ„๋‹จํ•˜๊ฒŒ ์ œ๋ชฉ์ด 5๊ธ€์ž ์ด์ƒ์ด์—ฌ์•ผ ํ•˜๊ณ  ๋นˆ๊ฐ’์ด ์•„๋‹ˆ์—ฌ์•ผ ํ•  ๊ฒฝ์šฐ vailidation์„ ์ž‘์„ฑํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
  • Article ๋ชจ๋ธ์— ๋Œ€ํ•ด validation ์ด๊ธฐ ๋•Œ๋ฌธ์— Article ๋ชจ๋ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • app/models/article.rb
class Article < ApplicationRecord
  validates :title, presence: true,
           length: {minimum: 5}
end
  • ์ดํ›„์— ๊ธ€ ์ž‘์„ฑ์„ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์—๋Ÿฌ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

ruby_on_rails_getting_start_blog_2-2

  • ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด validate๊ฐ€ ๋˜์—ˆ์ง€๋งŒ, ๊ทธ ์ดํ›„์— ์ž‘์—…์ด ์„ค์ •๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • validate๊ฐ€ ๋  ๊ฒฝ์šฐ ํ›„์† ์ž‘์—…์„ ๋‹ค์‹œ new view๋กœ ๋Œ์•„๊ฐ€๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
  • app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
    def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render 'new'
    end

  end

  def article_params
    params.require(:article).permit(:title, :text)
  end
end
  • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์ดํ›„์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๋ฉ”์‹œ์ง€๋ฅผ view์— ์•Œ๋ ค์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ด€๋ จ ๋กœ์ง์„ ์ž‘์„ฑํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
  • app/views/articles/new.html.erb
<h1>New Article</h1>

<%= form_with scope: :article, url: articles_path, local: true do |form| %>

  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@article.errors.count, "error") %> prohibited
        this article from being saved:
      </h2>
      <ul>
        <% @article.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>

<%= link_to 'Back', articles_path %>
  • ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ์žˆ๋‹ค๋ฉด ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ฒŒ ํ–ˆ์œผ๋ฉฐ, new ํ•จ์ˆ˜์— @article ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•จ์œผ๋กœ์จ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

ruby_on_rails_getting_start_blog_2-3

  • ์œ„์™€ ๊ฐ™์ด ์˜ˆ์˜๊ฒŒ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜์˜ค๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ†™ Article Update

  • bin/rails routes ๋กœ ํ•„์š”ํ•œ API๋ฅผ ํ™•์ธํ•ด๋ณด์ž.
Prefix       Verb   URI Pattern                    Controller#Action
edit_article GET    /articles/:id/edit(.:format)   articles#edit
article      PATCH  /articles/:id(.:format)        articles#update
  • articles_controller.rb
class ArticlesController < ApplicationController
  def edit
    @article = Article.find(params[:id])
  end

    def update
    @article = Article.find(params[:id])

    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end
  private

  def article_params
    params.require(:article).permit(:title, :text)
  end
end
  • app/views/articles/edit.html.erb
<h1>Edit article</h1>

<%= form_with(model: @article, local: true) do |form| %>

  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@article.errors.count, "error") %> prohibited
        this article from being saved:
      </h2>
      <ul>
        <% @article.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>

<% end %>

<%= link_to 'Back', articles_path %>
  • model์— @article ์„ ๊ฑด๋„ค์ฃผ๋ฉด ์ž๋™์œผ๋กœ ๊ฐ’๋“ค์„ ์ฑ„์›Œ ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ์ „์ฒด ์กฐํšŒ ํŽ˜์ด์ง€์—์„œ edit๋„ ํ•  ์ˆ˜ ์žˆ๋„๋ก link๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.
<h1>Edit article</h1>

<%= form_with(model: @article, local: true) do |form| %>

  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@article.errors.count, "error") %> prohibited
        this article from being saved:
      </h2>
      <ul>
        <% @article.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>

<% end %>

<%= link_to 'Back', articles_path %>
  • ๋‹จ์ผ ๊ธ€์„ ๋ณด๋Š” show.html.erb์—๋„ Edit๋ฅผ ์ถ”๊ฐœํ•ด์ฃผ๋ฉด ๋์ž…๋‹ˆ๋‹ค.
<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>

<p>
  <strong>Text:</strong>
  <%= @article.text %>
</p>

<%= link_to 'Edit', edit_article_path %>
<%= link_to 'Back', articles_path %>

view ์ค‘๋ณต ์ œ๊ฑฐ

  • ์ผ๋‹จ edit์™€ new๋Š” ๊ต‰์žฅํžˆ ๋น„์Šทํ•˜๊ฒŒ ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ค‘๋ณต ๋ถ€๋ถ„๋งŒ ๋”ฐ๋กœ ๋นผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
  • app/views/articles/_form.html.erb
<%= form_with model: @article, local: true do |form| %>

  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@article.errors.count, "error") %> prohibited
        this article from being saved:
      </h2>
      <ul>
        <% @article.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>

<% end %>
  • form_with์„ ์ œ์™ธํ•œ ๋ถ€๋ถ„์—์„œ ๋ชจ๋‘ ๋น„์Šทํ•œ๋ฐ, ์—ฌ๊ธฐ์„œ ๊ณตํ†ต์ ์ธ ๋ถ€๋ถ„์€ @article์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ด๊ธฐ ๋–„๋ฌธ์— model๋กœ ์„ ์–ธํ•˜๋ฉด Rails๊ฐ€ ์•Œ์•„์„œ ์‚ฌ์šฉํ•  URI์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค.
  • app/views/articles/new.html.erb
<h1>New Article</h1>

<%= render 'from' %>

<%= link_to 'Back', articles_path %>
  • app/views/articles/edit.html.erb
<h1>Edit article</h1>

<%= render 'from' %>

<%= link_to 'Back', articles_path %>

โšฐ๏ธ Delete Article

  • bin/rails routes ๋กœ api๋ฅผ ์กฐํšŒํ•˜๋ฉด ์ „์ฒด ์กฐํšŒ API๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
Prefix   Verb      URI Pattern                Controller#Action
articles DELECT    /articles/:id(.:format)    articles#destory
  • articles_controller.rb
class ArticlesController < ApplicationController
  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to articles_path
  end
end
  • app/views/articles/index.html.erb
<h1>Listing articles</h1>

<%= link_to 'New article', new_article_path %>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th></th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Delete', article_path(article),method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</table>

๐Ÿ˜‡ ๊ฒฐ๋ก 

  • ๋ฃจ๋น„๊ฐ€ ํ™•์‹คํžˆ ๊ธฐ๋ณธ์ ์ธ API๋“ค์„ ๋ชจ๋‘ ๋งŒ๋“ค์–ด ์ฃผ๊ธฐ ๋–„๋ฌธ์— ๊ฐœ๋ฐœ ์ƒ์‚ฐ์„ฑ์€ ๋Š˜์–ด๋‚˜๊ฒŒ ๋˜๋Š”๊ฒƒ ๊ฐ™๋‹ค.
  • ๋˜ํ•œ Link ์—ฐ๊ฒฐ๋„ helper๋“ค์ด ๋ชจ๋‘ ๋„์™€์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ถ…์ด url์„ ๋ช…์‹œํ•˜์ง€ ์•Š์•„๋„ ์‰ฝ๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๊ธฐ๋„ ํ•œ๊ฑฐ ๊ฐ™๋‹ค.
  • validate๋„ ๋ชจ๋ธ์—์„œ ์‰ฝ๊ฒŒ ์ •์˜ํ• ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ข€๋” ๋ชจ๋ธ ์นœํ™”์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™๋‹ค.
  • ๋‹ค์ŒํŽธ์—์„œ๋Š” Article CRUD๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ, Commenet ๊ธฐ๋Šฅ์„ ๊ฐœ๋ฐœํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜‚

์ถœ์ฒ˜

Getting Started with Rails - Ruby on Rails Guides

728x90
728x90