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 ํด์ค์ผ ํฉ๋๋ค.
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ํธ์ ๋ณด์๊ด๋ จํด์ ๊ฐ๋จํ๊ฒ ์ด์ผ๊ธฐ ํด๋ณผ๋ ค๊ณ ํฉ๋๋ค
์ถ์ฒ
728x90
728x90
'Backend > RubyOnRails' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[RubyOnRails Guides] Active Record Basics (0) | 2021.02.17 |
---|---|
RubyOnRails Getting Start(Blog ๋ง๋ค๊ธฐ) 4ํธ(๋ง์ง๋ง๐จโ๐จ) (0) | 2021.02.16 |
RubyOnRails Getting Start(Blog ๋ง๋ค๊ธฐ) 2ํธ (0) | 2021.02.16 |
RubyOnRails Getting Start(Blog ๋ง๋ค๊ธฐ) 1ํธ (0) | 2021.02.16 |
[RubyOnRails] rails new File & Folder (0) | 2021.02.16 |