概要
ログインしているユーザーが記事にコメントできるAPIを実装
.
テストはこちら
.

.
【使用したgem】
devise_token_auth
ログイン情報をtokenで認証active_model_selializer
JSONオブジェクトを作成するオブジェクトの作成を支援するライブラリ。
今回の場合、Railsのビューは使わない。
実装手順
User, Article, Commentの3つのモデルが必要。
.
User, Articleはすでに作ってあるので
.
Commentモデルの作成から始める。
.
コメントのcontent
はstring
型に指定。text
でも可。
.
データベースにPostgresqlを使っているので
文字数制限の記述をしなければあまり違いはない。
.
mysqlだとstring型は255文字制限があるので
コメントの文字数の制限をしたくなければtext型が無難。
.
content:string
.
UserとArticleとはどちらも1対多の関係なので
.
user:references article:references
.
このように記述して外部キーを作る
.
$ be rails g model Comment content:string user:references article:references
.
各modelにアソシエーションを記述。
.
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
include DeviseTokenAuth::Concerns::User
has_many :articles, dependent: :destroy
has_many :comments, dependent: :destroy
end
.
class Article < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
end
.
contentが空の場合エラーになるようにcommentモデルに記述。
.
validates :content, presence: true
.
class Comment < ApplicationRecord
belongs_to :user
belongs_to :article
validates :content, presence: true
end
.
次にルーティングの設定をします。
.
Rails.application.routes.draw do
namespace :v1 do
mount_devise_token_auth_for "User", at: "auth", controllers: {
registrations: "v1/auth/registrations",
}
resources :articles do
resources :comments, controller: "articles/comments"
end
end
end
.
記事にコメントをするので
.
どの記事にコメントするのかURLでわかるように
.
ルーティングを変更する。
.
resources :articles
配下にresources :comments
を記述。
.
その他ルーティングに関してはこちらの記事。
.
次にコントローラーを作成し
.
APIを実装していきます。
.
完成形がこちら。
.
class V1::Articles::CommentsController < V1::BaseApiController
def index
article = Article.find(params[:article_id])
comments = article.comments
render json: comments
end
def create
article = Article.find(params[:article_id])
comment = current_user.comments.new(comment_params)
comment.article = article
comment.save!
render json: comment
end
def destroy
comment = current_user.comments.find(params[:id])
comment.destroy!
render json: comment
end
def show
comment = Comment.find(params[:id])
render json: comment
end
def update
comment = current_user.comments.find(params[:id])
comment.update!(comment_params)
render json: comment
end
private
def comment_params
params.require(:comment).permit(:content)
end
end
.
indexメソッド
def index
article = Article.find(params[:article_id])
comments = article.comments
render json: comments
end
.
article = Article.find(params[:article_id])
で記事を指定。
.
comments = article.comments
で指定した記事のコメントを
.
comments
に代入。
.
render json: comments
でJSONで返すように設定。
.
createメソッド
def create
article = Article.find(params[:article_id])
comment = current_user.comments.new(comment_params)
comment.article = article
comment.save!
render json: comment
end
private
def comment_params
params.require(:comment).permit(:content)
end
.
コメントを作る時には
.
content
,user_id
,article_id
が必要になる。
.
article = Article.find(params[:article_id])
で記事を指定。
.
current_user.comments.new(comment_params)
で
.
ログインしているユーザーのコメントを作成。
.
content
とuser_id
が入る。
.
comment_params
はストロングパラメーターで
.
commentモデルのcontentカラムだけ許可するように設定してある。
.
あと必要なのはarticle_id
なので
.
comment.article = article
で設定。
.
comment.save!
保存して
.
render json: comment
JSONで返すように設定。
.
destroyメソッド
def destroy
comment = current_user.comments.find(params[:id])
comment.destroy!
render json: comment
end
.
コメントの削除機能は
.
ログインしているユーザーのコメントだけ削除できるようにする。
.
comment = current_user.comments.find(params[:id])
で
.
ログインしているユーザーのコメントを指定。
.
comment.destroy!
削除してrender json: comment
JSONで返すように設定。
showメソッド
def show
comment = Comment.find(params[:id])
render json: comment
end
コメントの詳細画面はcomment = Comment.find(params[:id])
コメントを指定してrender json: comment
JSONで返すように設定。
def update
comment = current_user.comments.find(params[:id])
comment.update!(comment_params)
render json: comment
end
.
コメントの更新機能は
.
今まで出てきたやつを組み合わせて完了。
.
関連記事


コメント