現在railsアプリでいいね機能を実装しています。
いいねしたユーザー一覧を取得したいのですがうまく取得できません。
既にいいねすることは実装済みで、一覧のみ取得できません。
どなたか分かる方いませんでしょうか。
recruitments/show.html.haml --省略-- - @recruitment.favorited_users.each do |user| %li.good = user.name
recruitment.rb class Recruitment < ApplicationRecord belongs_to :user has_many :favorites has_many :favorited_users, :through => :favorites, :source => :user def favorited_by?(user) favorites.where(user_id: user.id).exists? end end
user.rb class User < ApplicationRecord has_many :recruitments has_many :favorites has_many :favorite_recruitments, :through => :favorites, :source => :recruitments end
recruitments_controller.rb class RecruitmentsController < ApplicationController def index @recruitments = Recruitment.all.page(params[:page]).per(5).order("updated_at DESC") end def new @recruitments = Recruitment.new end def create @recruitments = Recruitment.new(recruitment_params) if @recruitments.save redirect_to users_path else render "new" end end def show @recruitment = Recruitment.find(params[:id]) @user = @recruitment.user @fovorites = @recruitment.favorites @favorite = Favorite.new end def destroy recruitment = Recruitment.find(params[:id]) recruitment.destroy redirect_to "/users/#{current_user.id}" end def edit @recruitment = Recruitment.find(params[:id]) end def update recruitment = Recruitment.find(params[:id]) recruitment.update(recruitment_edit_params) redirect_to :action => 'show' end private def recruitment_params params.permit(:recruitment_title, :recruitment_type, :study_period, :recruitment_overview, :production_period, :production).merge(user_id: current_user.id) end def recruitment_edit_params params.require(:recruitment).permit(:recruitment_title, :recruitment_type, :study_period, :recruitment_overview, :production_period, :production).merge(user_id: current_user.id) end end
recruitments/show.html.haml でリクルートメントにいいねしたユーザー一覧を表示する認識です。
recruitments/show.html.hamlを表示を扱っているControllerのコードを載せていただけると参考になります。およそrecruitments_controller.rbとかってファイル名かと。
またControllerは見れていませんが、既存のコードもそれほど誤った記述はなさそうです。現在はどのようなエラーになりますか?または期待しない表示になっていますか?
返信ありがとうございます。
recruitments_controller.rbのコードを追加しました。
お手数ですが、ご確認お願いいたします。
ありがとうございます。現在は、エラーが出ていますか?
現在エラーは無くビューに何も表示されない状態です。
回答1件
あなたの回答
tips
プレビュー