@814hiros

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

「504 Gateway Time-out」の解決方法及びコードチェックのお願い

Q&A

Closed

解決したいこと

投稿に対するお気に入り機能を付与するためにコードを打ったあと、ローカルのブラウザにて動作を確認しようとしたところ
「504 Gateway Time-out」と表示され、アプリが開けません。

他アプリでは開けますので、コードに誤りがあるかご指摘いただけますと幸いです。

発生している問題・エラー

504 Gateway Time-out

該当するソースコード

【confing/routes.rb】

Rails.application.routes.draw do
  root to: 'toppages#index'

  get 'login', to: 'sessions#new'
  post 'login', to: 'sessions#create'
  delete 'logout', to: 'sessions#destroy'

  get 'signup', to: 'users#new'
  resources :users, only: [:index, :show, :new, :create] do
    member do
      get :followings
      get :followers
    end
  end

  resources :microposts, only: [:create, :destroy]
  resources :relationships, only: [:create, :destroy]
  resources :favorites, only:[:create, :destroy]
end

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

【views/_favorite_button.html.erb】


  <% if current_user.favorite?(micropost) %>
    <%= form_with(model: current_user.favorites.find_by(:micropost_id), local: true, method: :delete) do |f| %>
      <%= hidden_field_tag :micropost_id %>
      <%= f.submit 'Unfavorite', class: 'btn btn-danger btn-block' %>
    <% end %>
  <% else %>
    <%= form_with(model: current_user.favorites.build, local: true) do |f| %>
      <%= hidden_field_tag :micropost_id %>
      <%= f.submit 'Favorite', class: 'btn btn-primary btn-block' %>
    <% end %>
  <% end %>


ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

【controllers/favorites_controller.rb】
class FavoritesController < ApplicationController

  before_action :require_user_logged_in

  def create
    micropost = Micropost.find(params[:micropost_id])
    current_user.favorite(micropost)
    flash[:success] = 'お気に入りしました。'
    redirect_to micropost
  end

  def destroy
    micropost = Micropost.find(params[:micropost_id])
    current_user.unfavorite(user)
    flash[:success] = 'お気に入りを解除しました。'
    redirect_to micropost
  end
end


-------------------------------------------------------


【models/user.rb】
class User < ApplicationRecord
  before_save { self.email.downcase! }
  validates :name, presence: true, length: { maximum: 50 }
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
                    uniqueness: { case_sensitive: false }
  has_secure_password

  has_many :microposts
  has_many :relationships
  has_many :followings, through: :relationships, source: :follow
  has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id'
  has_many :followers, through: :reverses_of_relationship, source: :user
  has_many :favorites
  has_many :favorites, through: :favorites, source: :micropost
  has_many :reverses_of_favorite, class_name: 'Favorite', foreign_key: 'micropost_id'

  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)
  end

  def feed_microposts
    Micropost.where(user_id: self.following_ids + [self.id])
  end


  def like
  unless self == other_micropost
      self.favorites.find_or_create_by(micropost_id: other_user.id)
  end
  end

  def unlike
    favorite = self.favorite.find_by(micropost_id: other_user.id)
    favorite.destroy if favorite
  end

  def favorite?(other_user)
    self.microposts.include?(other_user)
  end



end


### 自分で試したこと
lsof -i :8080→kill -9 PID番号 と入力後、再起動を試しましたが変わらずでした、、
ご教授いただけますと幸いです。
よろしくお願いいたします。
0 likes

1Answer

504 Gateway Time-out

が出るときはプログラム側ではなく、WEBサーバー設定側かなと思います。

0Like

Comments

  1. @814hiros

    Questioner

    続けてありがとうございます!
    お気に入り機能の付与する際入力したコードを削除したところ、無事反映されました。
    また半分くらいからやり直しです、、

    続けての質問で大変恐縮ですが、以下のコードはフォロー・フォロワーの数を反映されるためだと思われますが、お気に入り数を表示されるためには同ファイルで別アクションで定義しなければいけないでしょうか?

    def counts(user)
    @count_microposts = user.microposts.count
    @count_followings = user.followings.count
    @count_followers = user.followers.count
    end

    よろしくお願いいたします。

Your answer might help someone💌