分からないこと
ユーザーのマイページで、自分が投稿したもののみを一覧表示させ、投稿がない場合は「投稿がありません」の記述に変更したい
現状、current_contracted_side_user.id == @post.contracted_side_user_idの部分がうまく行ってないと思われます
条件分岐のやり方は問題ないと思うので、idの渡し方の問題かなと思いますがうまくいきません
また、不足するコードがありましたら、ご連絡をお願いします
コントローラー
class ContractedSideUsersController < ApplicationController before_action :authenticate_any! def show @posts = Post.where(params[contracted_side_user_id: current_contracted_side_user.id]) @post = Post.find_by(contracted_side_user_id: current_contracted_side_user.id) end private def authenticate_any! redirect_to new_contracted_side_user_session_path unless contracted_side_user_signed_in? end end
ビュー
<% if @posts.present? && current_contracted_side_user.id == @post.contracted_side_user_id %> <% @posts.each do |post| %> <%= render "shared/company_detail", post: post %> <div class="post-btn-form"> <div class="posted-edit-btn-form"> <%= link_to edit_post_path(post), method: :get, class:"posted-edit-btn" do %> <i class="fas fa-edit">編集する</i> <% end %> </div> <div class="posted-delete-btn-form"> <%= link_to post_path(post), method: :delete, class:"posted-delete-btn" do %> <i class="far fa-trash-alt">削除する</i> <% end %> </div> </div> <% end %> <% else %> <div class="no-search-message"> <i class="fas fa-exclamation-triangle">投稿がありません</i> </div> <% end %> コード
仮設、検証
@postsはwhereで取得しているため、contracted_side_user.id == @posts.contracted_side_user_idとすることができない
@post = Post.find_by(contracted_side_user_id: current_contracted_side_user.id)とすることで、最初にヒットした(自分の投稿の)idが存在すれば、表示できるようにする
検証結果
「投稿がありません」の画面が表示される
環境
ruby 2.7.2
Rails 6.1.3.1
VSCode
MySQL
post.rb
class Post < ApplicationRecord has_one_attached :image belongs_to :contracted_side_user has_many :favorites, dependent: :destroy has_many :reviews, dependent: :destroy has_many :consignment_side_users, through: :favirites has_many :consignment_side_users, through: :reviews VALID_POST_CODE_REGEX = /\A\d{3}-\d{4}\z/.freeze VALID_EAMIL_REGEX = /\A[A-Za-z0-9.+_-]+@([A-Za-z0-9_-]+.)+[A-Za-z]{2,}\z/.freeze VALID_PHONE_NUMBER_REGEX = /\A\d{10,11}\z/.freeze VALID_COMPANY_URL_REGEX = /\A#{URI::regexp(%w(http https))}\z/.freeze with_options presence: true do validates :image validates :industry_id, numericality: { other_than: 1, message: 'は---以外を選択してください' } validates :company_name validates :post_code, format: { with: VALID_POST_CODE_REGEX, message: 'を正しく入力してください' } validates :prefecture_id, numericality: { other_than: 1, message: 'は---以外を選択してください' } validates :address validates :employee_number validates :representative_name validates :email, format: { with: VALID_EAMIL_REGEX, message: 'を正しく入力してください' } validates :represent_phone_number, format: { with: VALID_PHONE_NUMBER_REGEX, message: 'を正しく入力してください' } validates :business_detail, length: { maximum: 500 } end validates :company_url, format: { with: VALID_COMPANY_URL_REGEX, message: 'を正しく入力してください' }, allow_blank: true validates :direct_phone_number, format: { with: VALID_PHONE_NUMBER_REGEX, message: 'を正しく入力してください' }, allow_blank: true extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :industry belongs_to :prefecture end
contracted_side_user.rb
class ContractedSideUser < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :posts VALID_EAMIL_REGEX = /\A[A-Za-z0-9.+_-]+@([A-Za-z0-9_-]+.)+[A-Za-z]{2,}\z/.freeze VALID_PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]+\z/i.freeze VALID_NAME_REGEX = /\A[ぁ-んァ-ヶ一-龥々ー]+\z/.freeze VALID_NAME_RUBY_REGEX = /\A[ァ-ヶ]+\z/.freeze with_options presence: true do validates :email, uniqueness: { case_sensitive: true }, format: { with: VALID_EAMIL_REGEX, message: 'を正しく入力してください' } validates :password, format: { with: VALID_PASSWORD_REGEX, message: 'を正しく入力してください' } validates :last_name, format: { with: VALID_NAME_REGEX, message: 'を正しく入力してください' } validates :first_name, format: { with: VALID_NAME_REGEX, message: 'を正しく入力してください' } validates :last_name_ruby, format: { with: VALID_NAME_RUBY_REGEX, message: 'を正しく入力してください' } validates :first_name_ruby, format: { with: VALID_NAME_RUBY_REGEX, message: 'を正しく入力してください' } end end
回答1件
あなたの回答
tips
プレビュー