前提・実現したいこと
上記の画像のように、画像を投稿した最新のものには、非同期フォローが出来ているんですけど、
過去に投稿したものには非同期で反映されません。(リロードすると反映されます)
発生している問題・エラーメッセージ
検証ツールのconsoleにて、非同期が出来ていない部分を確認してみると、
500 (Internal Server Error)というエラーが出ていました。
該当のソースコード
####モデル
user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :post_images, dependent: :destroy has_many :favorites, dependent: :destroy has_many :fav_post_images, through: :favorites, source: :post_image has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_post_images, through: :favorites, source: :post_image has_many :post_comments, dependent: :destroy has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user # 2~8文字以内で名前が入っているかの確認 validates :name, presence: true, length: { in: 2..8 }, uniqueness: true # 自己紹介 100文字以内 validates :introduction, length: { maximum: 100 } # 可能なコスプレ 100文字以内 validates :like_cos, length: { maximum: 100 } # refile定義 attachment :profile_image # フォロー機能のメソッド def follow(other_user) unless self == other_user # フォローしようとしている人が自分自身ではないか # フォローしようとしている人が自分以外ならフォローする self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship # フォローしていたらアンフォローする end def following?(other_user) self.followings.include?(other_user) # other_userが含まれていたらtrueを返す end end
relationship.rb
class Relationship < ApplicationRecord belongs_to :user # userモデルを参照する belongs_to :follow, class_name: 'User' validates :user_id, presence: true validates :follow_id, presence: true end
post_image.rb
class PostImage < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :fav_users, through: :favorites, source: :user has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user has_many :post_comments, dependent: :destroy # refile定義 attachment :real_image attachment :cosplay_image validates :real_image, presence: true validates :cosplay_image, presence: true # 投稿を降順に並び替えし、最新のものを上にくるようにする default_scope -> { order(created_at: :asc) } end
####コントローラー
users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) @post_images = @user.post_images end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update(user_params) redirect_to user_path(@user.id) else render 'edit' end end def following @user = User.find(params[:id]) @users = @user.followings render 'show_follow' end def followers @user = User.find(params[:id]) @users = @user.followers render 'show_follower' end private def user_params params.require(:user).permit(:name, :profile_image, :introduction, :like_cos) end end
relationships_controller.rb
class RelationshipsController < ApplicationController before_action :set_user def create following = current_user.follow(@user) following.save respond_to do |format| format.html { redirect_to @user } format.js end end def destroy following = current_user.unfollow(@user) following.destroy respond_to do |format| format.html { redirect_to @user } format.js end end private def set_user @user = User.find(params[:follow_id]) end end
post_images_controller.rb
class PostImagesController < ApplicationController def new @post_image = PostImage.new end # 投稿データの保存 def create @post_image = PostImage.new(post_image_params) @post_image.user_id = current_user.id if @post_image.save redirect_to post_images_path else render 'new' end end def index @post_images = PostImage.page(params[:page]).reverse_order end def show @post_image = PostImage.find(params[:id]) @post_comment = PostComment.new end def destroy @post_image = PostImage.find(params[:id]) @post_image.destroy redirect_to post_images_path end private # 投稿データのストロングパラメータ def post_image_params params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count) end end
####ビュー
relationships/_follow_button.rb
<% unless current_user == user %> <div id="follow_form"> <% if current_user.following?(user) %> <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }, remote: true) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'フォロー中' %> <% end %> <% else %> <%= form_for(current_user.relationships.build, remote: true) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'フォローする' %> <% end %> <% end %> </div> <% end %>
create.js.erb
$("<%= '#follow_form' %>").html('<%= escape_javascript(render("relationships/follow_button", user: @user )) %>');
destroy.js.erb
$("<%= '#follow_form' %>").html('<%= escape_javascript(render("relationships/follow_button", user: @user )) %>');
post_images/index.html.erb
<% @post_images.each do |post_image| %> <%= render 'relationships/follow_button', user: post_image.user %> <% end %>
補足情報(FW/ツールのバージョンなど)
ruby 2.5.7p206
Rails 5.2.4.1
###追記
####_follow_button.html.erb
<% unless current_user == user %> <div id="follow_form_<%= user.id %>"> <% if current_user.following?(user) %> <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }, remote: true) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'フォロー中' %> <% end %> <% else %> <%= form_for(current_user.relationships.build, remote: true) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'フォローする' %> <% end %> <% end %> </div> <% end %>
####create.js.erb, destroy.js.erb
$("<%= @userid %>").html('<%= escape_javascript(render("relationships/follow_button", user: @user )) %>');
####relationships_controller.rb
class RelationshipsController < ApplicationController before_action :set_user def create following = current_user.follow(@user) following.save respond_to do |format| format.html { redirect_to @user } format.js end end def destroy following = current_user.unfollow(@user) following.destroy if following respond_to do |format| format.html { redirect_to @user } format.js end end private def set_user @user = User.find(params[:follow_id]) @userid = "#follow_form_#{@user.id}" end end
回答2件
あなたの回答
tips
プレビュー