Backend/RubyOnRails

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

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

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

๐Ÿ’ผ ์„œ๋ก 

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

Comment ๋ชจ๋ธ ์ถ”๊ฐ€

  • ๋ชจ๋ธ ์ถ”๊ฐ€ ๋ช…๋ น์–ด
$ bin/rails generate model Comment commenter:string body:text article:references
  • db / migrate / 20140120201010_create_comments.rb
class CreateComments < ActiveRecord::Migration[5.2]
  def change
    create_table :comments do |t|
      t.string :commenter
      t.text :body
      t.references :article, foreign_key: true

      t.timestamps
    end
  end
end
  • app/models/comment.rb
class Comment < ApplicationRecord
  belongs_to :article
end
  • ์ผ๋‹จ belongs_to๋ฅผ ์ด์šฉํ•ด์„œ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•œ๊ฒƒ์ด๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.
bin/rails db:migrate
  • ์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด DB Migrate๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

๐Ÿ”— ๋ชจ๋ธ ์—ฐ๊ฒฐ

  • Comment๋Š” ํ•˜๋‚˜์˜ Article์— ์†ํ•˜๊ณ  ํ•˜๋‚˜์˜ Article์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ Comment๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ Article ๋ชจ๋ธ์— ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘์„ ์ง„ํ–‰ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
class Article < ApplicationRecord
  has_many :comments
  validates :title, presence: true,
           length: {minimum: 5}
end

โž• ๊ฒฝ๋กœ ์ถ”๊ฐ€

  • Rails๊ฐ€ article์™€ Comments์˜ ๊ด€๊ณ„๋ฅผ ์•Œ๋„๋ก ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.
Rails.application.routes.draw do
  get 'welcome/index'

  resources :articles do
    resources :comments
  end

  root 'welcome#index'
end

Controller ์ƒ์„ฑ

$ bin/rails generate controller Comments
  • bin/rails routes ๋กœ API ํ™•์ธํ•ด๋ณด์ž.
Prefix Verb   URI Pattern                                                                              Controller#Action
         article_comments GET    /articles/:article_id/comments(.:format)                                                 comments#index
                          POST   /articles/:article_id/comments(.:format)                                                 comments#create
      new_article_comment GET    /articles/:article_id/comments/new(.:format)                                             comments#new
     edit_article_comment GET    /articles/:article_id/comments/:id/edit(.:format)                                        comments#edit
          article_comment GET    /articles/:article_id/comments/:id(.:format)                                             comments#show
                          PATCH  /articles/:article_id/comments/:id(.:format)                                             comments#update
                          PUT    /articles/:article_id/comments/:id(.:format)                                             comments#update
                          DELETE /articles/:article_id/comments/:id(.:format)                                             comments#destroy
                 articles GET    /articles(.:format)                                                                      articles#index
                          POST   /articles(.:format)                                                                      articles#create
              new_article GET    /articles/new(.:format)                                                                  articles#new
             edit_article GET    /articles/:id/edit(.:format)                                                             articles#edit
                  article GET    /articles/:id(.:format)                                                                  articles#show
                          PATCH  /articles/:id(.:format)                                                                  articles#update
                          PUT    /articles/:id(.:format)                                                                  articles#update
                          DELETE /articles/:id(.:format)                                                                  articles#destroy

๋Œ“๊ธ€ ์ž‘์„ฑ ๊ธฐ๋Šฅ

  • app/views/articles/show.html.erb
<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>

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

<h2>Comments</h2>
<% @article.comments.each do |comment| %>
  <p>
    <strong>Commenter:</strong>
    <%= comment.commenter %>
  </p>

  <p>
    <strong>Comment:</strong>
    <%= comment.body %>
  </p>
<% end %>

<h2>Add a comment:</h2>
<%= form_with(model: [@article, @article.comments.build], local: true) do |form| %>
  <p>
    <%= form.label :commenter %><br>
    <%= form.text_field :commenter %>
  </p>
  <p>
    <%= form.label :body %><br>
    <%= form.text_area :body %>
  </p>
  <p>
    <%= form.submit %>
  </p>
<% end %>

<%= link_to 'Edit', edit_article_path %>
<%= link_to 'Back', articles_path %>
  • app/controllers/comments_controller.rb
class CommentsController < ApplicationController
  def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.create(comment_params)

    redirect_to article_path(@article)
  end

  private
  def comment_params
    params.require(:comment).permit(:commenter, :body)
  end
end
  • ์—ฐ๊ด€๊ด€๊ณ„๋กœ ๋งคํ•‘๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์ด create๋ฅผ ํ• ๋•Œ Article์„ ์ฐพ์•„์„œ comment๋ฅผ create ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ruby_on_rails_getting_start_blog_3-1

comment ๋ฆฌํŒฉํ† ๋ง

  • ํ”„๋ก ํŠธ์—์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒƒ ์ฒ˜๋Ÿผ ๋ถ„๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
  • ์ฝ”๋ฉ˜ํŠธ๊ฐ€ ๋ณด์—ฌ์ง€๋Š” ๋ถ€๋ถ„๊ณผ ์ž…๋ ฅํ•˜๋Š” form์„ ๋ถ„๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
  • app/views/comments/_comment.html.erb
<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>

<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>
  • app/views/comments/_form.html.erb
<%= form_with(model: [ @article, @article.comments.build ], local: true) do |form| %>
  <p>
    <%= form.label :commenter %><br>
    <%= form.text_field :commenter %>
  </p>
  <p>
    <%= form.label :body %><br>
    <%= form.text_area :body %>
  </p>
  <p>
    <%= form.submit %>
  </p>
<% end %>
  • app/views/articles/show.html.erb
<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>

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

<h2>Comments</h2>
<%= render @article.comments %>

<h2>Add a comment:</h2>
<%= render 'comments/form' %>

<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>

โšฐ๏ธ ์ฝ”๋ฉ˜ํŠธ ์‚ญ์ œ ๊ธฐ๋Šฅ

  • bin/rails routes ๋กœ delete ๊ธฐ๋Šฅ api๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
Prefix          Verb   URI Pattern                                  Controller#Action
article_comment DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
  • controller ๋กœ์ง๊ณผ view ๋กœ์ง์„ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
  • app/views/comments/_comment.html.erb
<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>

<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>

<p>
  <%= link_to '์‚ญ์ œ', [comment.article, comment],
              method: :delete,
              data: { confirm: '์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?' } %>
</p>
  • app/controllers/comments_controller.rb
class CommentsController < ApplicationController
  def destroy
    @article = Article.find(params[:article_id])
    @comment = @article.comments.find(params[:id])
    @comment.destroy

    redirect_to article_path(@article)
  end
end

๐Ÿ“ฆ Article ์‚ญ์ œ ์‹œ Comment๋„ ๊ฐ™์ด ์‚ญ์ œ

  • dependent ์˜ต์…˜์„ ์ด์šฉํ•ด ์†์‰ฝ๊ฒŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • app/models/article.rb
class Article < ApplicationRecord
  has_many :comments, dependent: :destroy
  validates :title, presence: true,
           length: {minimum: 5}
end

๐Ÿ˜‡ ๊ฒฐ๋ก 

  • ์—ฐ๊ด€๊ด€๊ณ„๋„ ์†์‰ฝ๊ฒŒ ํ• ์ˆ˜ ์žˆ์œผ๋ฉฐ, CASECAD๋„ dependent ํ•˜๋‚˜๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ๋‹๋ณด์ด๊ธฐ๋„ ํ–ˆ๋‹ค.
  • ๋˜ํ•œ ์ข€๋” ์ง๊ด€์ ์œผ๋กœ article์•ˆ์— comment๋ผ๋Š” ์˜๋ฏธ๋กœ article.comment๋กœ ์ง์ ‘ ์กฐํšŒ, ์‚ญ์ œ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋Š”๊ฒƒ ๊ฐ™๋‹ค.
  • ๋งˆ์ง€๋ง‰ 4ํŽธ์€ ๋ณด์•ˆ๊ด€๋ จํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ด์•ผ๊ธฐ ํ•ด๋ณผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค

์ถœ์ฒ˜

Getting Started with Rails - Ruby on Rails Guides

728x90
728x90