前提
映画のAPIを利用した映画レビューサイトを作っています。
投稿後のレビューを各ユーザーページに表示することはできるのですが、
APIから取得した各映画のページに、レビューを表示することができず困っています。
おそらく、自身のデータベースに映画の情報が入っていないため、レビューのモデルに紐付けできず表示できないものと思われますが、APIを利用する場合、どのようにすれば成功するのかをお聞きできればと思います。
実現したいこと
- 各映画のページにレビューを表示したい
発生している問題・エラーメッセージ
エラーは特に出ていませんが、先述のとおり、レビューを各映画ページに表示することができません。
viewの書き方がよくない可能性も考えられるため、以下に関連していると思われるソースコードを記載させていただきました。
該当のソースコード
1. views/movies/各映画のshowページ
2. movieモデル
3. commentモデル
4. userモデル
5. comments_controller
1. views/movies/各映画のshowページ
<div class="row"> <aside class="col-md-4"> <section class="user_info"> <%= render 'shared/user_info' %> </section> <section class="comment_form"> <%= render 'shared/comment_form' %> </section> </aside> <div class="col-md-8"> <% if !@movie_db.nil? && @movie_db.comments.any? %> <h3>Comments (<%= @movie_db.comments.count %>)</h3> <ol class="comments"> <%= render @comments %> </ol> <%= will_paginate @comments %> <% end %> </div> </div>
2. movieモデル
class Movie < ApplicationRecord include HTTParty has_many :comments, dependent: :destroy has_many :users, through: :comments default_options.update(verify: false) default_params api_key: '実際にはコードが入ります', language: "ja-JP" format :json def self.search term base_uri 'https://api.themoviedb.org/3/search/movie' get("", query: { query: term}) end def self.details id get("https://api.themoviedb.org/3/movie/#{id}", query: {} ) end end
3. commentモデル
class Comment < ApplicationRecord belongs_to :user belongs_to :movie, optional: true default_scope -> { order(created_at: :desc) } validates :user_id, presence: true validates :content, presence: true, length: { maximum: 250} end
4. userモデル
class User < ApplicationRecord has_many :comments, dependent: :destroy has_many :movies, through: :comments attr_accessor :remember_token, :activation_token, :reset_token before_save :downcase_email before_create :create_activation_digest validates :name, presence: true, length: { maximum: 50} VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255}, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password validates :password, presence: true, length: { minimum: 6 }, allow_nil: true def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end def User.new_token SecureRandom.urlsafe_base64 end def remember self.remember_token = User.new_token update_attribute(:remember_digest, User.digest(remember_token)) end def authenticated?(remember_token) return false if remember_digest.nil? BCrypt::Password.new(remember_digest).is_password?(remember_token) end def forget update_attribute(:remember_digest, nil) end def authenticated?(attribute, token) digest = send("#{attribute}_digest") return false if digest.nil? BCrypt::Password.new(digest).is_password?(token) end def activate update_columns(activated: true, activated_at: Time.zone.now) end def send_activation_email UserMailer.account_activation(self).deliver_now end def create_reset_digest self.reset_token = User.new_token update_columns(reset_digest: User.digest(reset_token), reset_sent_at: Time.zone.now) end def send_password_reset_email UserMailer.password_reset(self).deliver_now end def password_reset_expired? reset_sent_at < 2.hours.ago end def feed Comment.where("user_id = ?", id) end private def downcase_email self.email.downcase! end def create_activation_digest self.activation_token = User.new_token self.activation_digest = User.digest(activation_token) end end
5. comments_controller
class CommentsController < ApplicationController before_action :logged_in_user, only: [:create, :destroy] before_action :correct_user, only: :destroy def create puts params @comment = current_user.comments.build(comment_params) if @comment.save flash[:success] = "Comment created!" redirect_to root_url else @feed_items = [] render 'static_pages/home' end end def destroy @comment.destroy flash[:success] = "Comment deleted" redirect_to request.referrer || root_url end private def comment_params params.require(:comment).permit(:content, :user_id, :movie_id) end def correct_user @comment = current_user.comments.find_by(id: params[:id]) redirect_to root_url if @comment.nil? end end
コメント
初心者であり、至らないところがありましたら、申し訳ございません。
ソースコードに関しましては、必要なものがありましたら、追記いたしますので、教えていただければ幸いです。
何卒宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。