前提・実現したいこと
購入者と売却者の情報を取り出すためこのようなアソシエーションを組んだのですが、うまくいきません。
どこの部分がうまくいっていないのでしょうか??
ぜひ皆様の知恵をお貸しください。
この記事を参考にしました。
https://qiita.com/fishmans0120/items/5b40fb2a057e4d722a67
発生している問題・エラーメッセージ
エラーメッセージ 2.5.3 :002 > user.bought_items Traceback (most recent call last): 1: from (irb):2 NameError (uninitialized constant User::Posts)
該当のソースコード
post.rb class Post < ApplicationRecord belongs_to :user belongs_to :seller, class_name: "User", foreign_key: "seller_id" belongs_to :buyer, class_name: "User", foreign_key: "buyer_id", optional: true has_one :post has_many :favorites, dependent: :destroy has_many :fav_posts, through: :favorites, source: :user mount_uploader :note, NoteUploader validates :title,:content,:teacher_name,:subject,:price,:note, presence: true attachment :post_image end
user.rb class User < ApplicationRecord has_many :posts has_many :sold_items, class_name: 'Posts', foreign_key: 'seller_id' has_many :bought_items, class_name: 'Posts', foreign_key: 'buyer_id' has_many :favorites, dependent: :destroy has_many :fav_posts, through: :favorites, source: :post has_one :card, dependent: :destroy # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :username, :user_image, presence: true attachment :user_image def already_favorited?(post) self.favorites.exists?(post_id: post.id) end end
class PostsController < ApplicationController before_action :authenticate_user!, only: [:new,:create,:edit,:update,:destroy,:done,:purchase,:buy] before_action :find_post, only: [:show, :edit, :update, :destroy, :purchase, :buy,:done] before_action :correct_user, only: [:destroy,:edit] require "payjp" def index @posts = Post.order(id: :desc).page(params[:page]).per(8) end def show end def new @post = Post.new end def create @post = current_user.posts.build(post_params) if @post.save flash[:success] = "正常に投稿されました!" redirect_to posts_path else flash.now[:danger] = "正常に投稿できませんでした。" render :new end end def edit end def update if @post.update(post_params) flash[:success] = 'ノートは正常に更新されました' redirect_to root_path else flash.now[:danger] = 'ノートは更新されませんでした' render :edit end end def destroy @post.destroy flash[:success] = 'メッセージを削除しました。' redirect_to root_path end def purchase # card = Card.where(user_id: current_user.id).first # if card.blank? # redirect_to controller: "cards", action: "new" # else # Payjp.api_key = ENV["PAYJP_SECRET_KEY"] # customer = Payjp::Customer.retrieve(card.customer_id) # @card = customer.cards.retrieve(card.card_id) # end end def buy Payjp.api_key = ENV["PAYJP_SECRET_KEY"] Payjp::Charge.create( amount: @post.price, card: params['payjp-token'], currency: 'jpy' ) @post.update(buyer_id: current_user.id) redirect_to action: "done" end def done end private def post_params params.require(:post).permit(:content,:title,:teacher_name,:subject,:post_image,:note,:price).merge(seller_id: current_user.id) end def find_post @post = Post.find(params[:id]) end def correct_user @post = current_user.posts.find_by(id: params[:id]) unless @post redirect_to new_user_session_path end end end
回答1件
あなたの回答
tips
プレビュー