0
0

More than 3 years have passed since last update.

micropostのカウント備忘録

Posted at

解決したい問題

投稿したmicropostの数をカウントしたいがエラーが起きる。ちなみに、user.controllerでは正常に使えているよう。

コード

全てのコントローラでmicropostのカウントメソッドを使うことができるよう、application.controllerに書いている。

application.controller
class ApplicationController < ActionController::Base
    include SessionsHelper

  private

  def require_user_logged_in
    unless logged_in?
      redirect_to login_url
    end
  end

  def counts(user)
    @count_microposts = user.microposts.count
  end

end

micropostのindexでmicropostカウントメソッドを使いたいのでindexメソッドにcounts(@user)を追加した。

micropost.controller
class MicropostsController < ApplicationController

  before_action :require_user_logged_in
  before_action :correct_user, only: [:edit,:update,:destroy,:show]

  def index 
    @microposts = current_user.microposts.order(id: :desc).page(params[:page]).per(10)
    counts(@user)
  end

  def update
    @micropost = Micropost.find(params[:id])

    if @micropost.update(micropost_params)
      flash[:success] = '保存されました。'
      redirect_to @micropost
    else
      flash.now[:danger] = '保存されませんでした。'
      render :edit
    end
  end

  def edit
    @micropost = Micropost.find(params[:id])
  end

  def show 
    @micropost=Micropost.find(params[:id])
  end

  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = '記事を投稿しました。'
      redirect_to root_url
    else
      @microposts = current_user.microposts.order(id: :desc).page(params[:page])
      flash.now[:danger] = '記事を投稿できませんでした。'
      render 'toppages/index'
    end
  end

  def destroy
    @micropost.destroy
    flash[:success] = '記事を削除しました。'
    redirect_back(fallback_location: microposts_path)

  end

   private

  def micropost_params
    params.require(:micropost).permit(:content,:title)
  end

  def correct_user
    @micropost = current_user.microposts.find_by(id: params[:id])
    unless @micropost
      redirect_to microposts_path
    end
  end

end
class MicropostsController < ApplicationController

  before_action :require_user_logged_in
  before_action :correct_user, only: [:edit,:update,:destroy,:show]

  def index 
    @microposts = current_user.microposts.order(id: :desc).page(params[:page]).per(10)
    counts(@micropost)
  end

  def update
    @micropost = Micropost.find(params[:id])

    if @micropost.update(micropost_params)
      flash[:success] = '保存されました。'
      redirect_to @micropost
    else
      flash.now[:danger] = '保存されませんでした。'
      render :edit
    end
  end

  def edit
    @micropost = Micropost.find(params[:id])
  end

  def show 
    @micropost=Micropost.find(params[:id])
  end

  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = '記事を投稿しました。'
      redirect_to root_url
    else
      @microposts = current_user.microposts.order(id: :desc).page(params[:page])
      flash.now[:danger] = '記事を投稿できませんでした。'
      render 'toppages/index'
    end
  end

  def destroy
    @micropost.destroy
    flash[:success] = '記事を削除しました。'
    redirect_back(fallback_location: microposts_path)

  end

   private

  def micropost_params
    params.require(:micropost).permit(:content,:title)
  end

  def correct_user
    @micropost = current_user.microposts.find_by(id: params[:id])
    unless @micropost
      redirect_to microposts_path
    end
  end
end
microposts/index.html.erb
<h3>記事一覧:<span class="badge badge-secondary"><%= @count_microposts %>件</span></h3>
<%= render 'microposts/microposts', microposts: @microposts %>

解決した方法

application.controllerの
@count_microposts = user.microposts.count@count_microposts = current_user.microposts.countにしたところ解決した。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0