[Ruby on Rails] 주석 기능 설치
목표
개발 환경
ruby 2.5.7
Rails 5.2.4.3
OS: macOS Catalina
전제 조건
※ ▶◯◯을 선택하면 설명 등이 나온다
잘 모르는 상황의 참고가 되었으면 좋겠습니다.
ruby 2.5.7
Rails 5.2.4.3
OS: macOS Catalina
전제 조건
※ ▶◯◯을 선택하면 설명 등이 나온다
잘 모르는 상황의 참고가 되었으면 좋겠습니다.
테이블 작성
단말기$ rails g model Comment user:references post:references comment:string
단말기$ rails db:migrate
모형의 수정
app/models/user.rbhas_many :comments, dependent: :destroy
app/models/post.rbhas_many :comments, dependent: :destroy
보태다
user는 여러 개의 공공 모델을 가지고 있기 때문에post는 여러 개의 공공 모델을 가지고 있다
has_many.
또한user,post가 없을 때commeent도 남을 필요가 없습니다
dependent: :destroy.
컨트롤러 생성
단말기$ rails g controller comments create destroy
app/controllers/comments_controller.rbclass CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(comment_params)
@comment.user_id = current_user.id
if @comment.save
redirect_to request.referer
else
@post_new = Book.new
@comments = @post.comments
redirect_to new_post_path
end
end
def destroy
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to request.referer
end
private
def comment_params
params.require(:comment).permit(:comment)
end
end
app/controllers/posts_controller.rb def show
@post = Post.find(params[:id])
@comment = Comment.new
@comments = @post.comments
end
라우팅 수정
config/routes.rb resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
end
보충1
상술한 기술은 이미 중첩되었다.
참새 둥지에 관해.이해하기 쉽다.
보충2
except는 제외라는 뜻이기 때문에 index 이외의 동작을 정의합니다.
뷰 변경
app/views/show.html.erb<h1>Posts#show</h1>
<span>現在ログイン中のユーザー:<%= current_user.name %></span>
<table>
<thead>
<tr>
<th>投稿者名</th>
<th>タイトル</th>
<th>本文</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><%= @post.user.name %></td>
<td><%= @post.title %></td>
<td><%= @post.body %></td>
<td><%= link_to "編集", edit_post_path(@post) %></td>
</tr>
</tbody>
</table>
<%= form_for [@post, @comment] do |f| %>
<%= f.text_area :comment, size: "40x5" %>
<%= f.submit '送信', class: "btn-sm btn-primary" %>
<% end %>
<table>
<thead>
<tr>
<th>コメント投稿者</th>
<th>コメント内容</th>
</tr>
</thead>
<tbody>
<% @comments.each do |comment| %>
<tr>
<td><%= comment.user.name %></td>
<td><%= comment.comment %></td>
<td><%= link_to "削除", post_comment_path(@post, comment), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
app/views/new.html.erb<table>
<thead>
<tr>
<th>投稿者名</th>
<th>タイトル</th>
<th>本文</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.user.name %></td>
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= link_to "詳細", post_path(post) %></td>
<td><%= link_to "編集", edit_post_path(post) %></td>
<td><%= link_to "削除", post_path(post), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
목표를 달성하다.
Reference
이 문제에 관하여([Ruby on Rails] 주석 기능 설치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/japwork/items/b2a7e61379c88a5e60c8
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
$ rails g model Comment user:references post:references comment:string
$ rails db:migrate
app/models/user.rb
has_many :comments, dependent: :destroy
app/models/post.rbhas_many :comments, dependent: :destroy
보태다user는 여러 개의 공공 모델을 가지고 있기 때문에post는 여러 개의 공공 모델을 가지고 있다
has_many.
또한user,post가 없을 때commeent도 남을 필요가 없습니다
dependent: :destroy.
컨트롤러 생성
단말기$ rails g controller comments create destroy
app/controllers/comments_controller.rbclass CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(comment_params)
@comment.user_id = current_user.id
if @comment.save
redirect_to request.referer
else
@post_new = Book.new
@comments = @post.comments
redirect_to new_post_path
end
end
def destroy
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to request.referer
end
private
def comment_params
params.require(:comment).permit(:comment)
end
end
app/controllers/posts_controller.rb def show
@post = Post.find(params[:id])
@comment = Comment.new
@comments = @post.comments
end
라우팅 수정
config/routes.rb resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
end
보충1
상술한 기술은 이미 중첩되었다.
참새 둥지에 관해.이해하기 쉽다.
보충2
except는 제외라는 뜻이기 때문에 index 이외의 동작을 정의합니다.
뷰 변경
app/views/show.html.erb<h1>Posts#show</h1>
<span>現在ログイン中のユーザー:<%= current_user.name %></span>
<table>
<thead>
<tr>
<th>投稿者名</th>
<th>タイトル</th>
<th>本文</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><%= @post.user.name %></td>
<td><%= @post.title %></td>
<td><%= @post.body %></td>
<td><%= link_to "編集", edit_post_path(@post) %></td>
</tr>
</tbody>
</table>
<%= form_for [@post, @comment] do |f| %>
<%= f.text_area :comment, size: "40x5" %>
<%= f.submit '送信', class: "btn-sm btn-primary" %>
<% end %>
<table>
<thead>
<tr>
<th>コメント投稿者</th>
<th>コメント内容</th>
</tr>
</thead>
<tbody>
<% @comments.each do |comment| %>
<tr>
<td><%= comment.user.name %></td>
<td><%= comment.comment %></td>
<td><%= link_to "削除", post_comment_path(@post, comment), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
app/views/new.html.erb<table>
<thead>
<tr>
<th>投稿者名</th>
<th>タイトル</th>
<th>本文</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.user.name %></td>
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= link_to "詳細", post_path(post) %></td>
<td><%= link_to "編集", edit_post_path(post) %></td>
<td><%= link_to "削除", post_path(post), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
목표를 달성하다.
Reference
이 문제에 관하여([Ruby on Rails] 주석 기능 설치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/japwork/items/b2a7e61379c88a5e60c8
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
$ rails g controller comments create destroy
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(comment_params)
@comment.user_id = current_user.id
if @comment.save
redirect_to request.referer
else
@post_new = Book.new
@comments = @post.comments
redirect_to new_post_path
end
end
def destroy
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to request.referer
end
private
def comment_params
params.require(:comment).permit(:comment)
end
end
def show
@post = Post.find(params[:id])
@comment = Comment.new
@comments = @post.comments
end
config/routes.rb
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
end
보충1상술한 기술은 이미 중첩되었다.
참새 둥지에 관해.이해하기 쉽다.
보충2
except는 제외라는 뜻이기 때문에 index 이외의 동작을 정의합니다.
뷰 변경
app/views/show.html.erb<h1>Posts#show</h1>
<span>現在ログイン中のユーザー:<%= current_user.name %></span>
<table>
<thead>
<tr>
<th>投稿者名</th>
<th>タイトル</th>
<th>本文</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><%= @post.user.name %></td>
<td><%= @post.title %></td>
<td><%= @post.body %></td>
<td><%= link_to "編集", edit_post_path(@post) %></td>
</tr>
</tbody>
</table>
<%= form_for [@post, @comment] do |f| %>
<%= f.text_area :comment, size: "40x5" %>
<%= f.submit '送信', class: "btn-sm btn-primary" %>
<% end %>
<table>
<thead>
<tr>
<th>コメント投稿者</th>
<th>コメント内容</th>
</tr>
</thead>
<tbody>
<% @comments.each do |comment| %>
<tr>
<td><%= comment.user.name %></td>
<td><%= comment.comment %></td>
<td><%= link_to "削除", post_comment_path(@post, comment), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
app/views/new.html.erb<table>
<thead>
<tr>
<th>投稿者名</th>
<th>タイトル</th>
<th>本文</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.user.name %></td>
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= link_to "詳細", post_path(post) %></td>
<td><%= link_to "編集", edit_post_path(post) %></td>
<td><%= link_to "削除", post_path(post), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
목표를 달성하다.
Reference
이 문제에 관하여([Ruby on Rails] 주석 기능 설치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/japwork/items/b2a7e61379c88a5e60c8
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
<h1>Posts#show</h1>
<span>現在ログイン中のユーザー:<%= current_user.name %></span>
<table>
<thead>
<tr>
<th>投稿者名</th>
<th>タイトル</th>
<th>本文</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><%= @post.user.name %></td>
<td><%= @post.title %></td>
<td><%= @post.body %></td>
<td><%= link_to "編集", edit_post_path(@post) %></td>
</tr>
</tbody>
</table>
<%= form_for [@post, @comment] do |f| %>
<%= f.text_area :comment, size: "40x5" %>
<%= f.submit '送信', class: "btn-sm btn-primary" %>
<% end %>
<table>
<thead>
<tr>
<th>コメント投稿者</th>
<th>コメント内容</th>
</tr>
</thead>
<tbody>
<% @comments.each do |comment| %>
<tr>
<td><%= comment.user.name %></td>
<td><%= comment.comment %></td>
<td><%= link_to "削除", post_comment_path(@post, comment), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
<table>
<thead>
<tr>
<th>投稿者名</th>
<th>タイトル</th>
<th>本文</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.user.name %></td>
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= link_to "詳細", post_path(post) %></td>
<td><%= link_to "編集", edit_post_path(post) %></td>
<td><%= link_to "削除", post_path(post), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
Reference
이 문제에 관하여([Ruby on Rails] 주석 기능 설치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/japwork/items/b2a7e61379c88a5e60c8텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)