前提・実現したいこと
フォームオブジェクトを用いた購入機能の実装。
コントローラーでエラーが出ています。
発生している問題・エラーメッセージ
エラーメッセージ ActiveModel::UnknownAttributeError in OrdersController#create unknown attribute 'recruitment_id' for Order.
ターミナル Recruitment Load (0.5ms) SELECT `recruitments`.* FROM `recruitments` WHERE `recruitments`.`id` = 2 LIMIT 1 ↳ app/controllers/orders_controller.rb:23:in `recruitment_params' Unpermitted parameters: :number, :exp_month, :exp_year, :cvc User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/controllers/orders_controller.rb:27:in `order_params' Completed 500 Internal Server Error in 53ms (ActiveRecord: 12.8ms | Allocations: 23920) ActiveModel::UnknownAttributeError (unknown attribute 'recruitment_id' for Order.): app/controllers/orders_controller.rb:10:in `create'
該当のソースコード
コントローラー
class OrdersController < ApplicationController before_action :authenticate_user!, only: [:index ] before_action :recruitment_params, only: [:index, :create ] def index @order = Order.new end def create @order = Order.new(order_params) if @order.valid? pay_recruitment @order.save redirect_to root_path(@order) else render :index end end private def recruitment_params @recruitment = Recruitment.find(params[:recruitment_id]) end def order_params params.require(:order).permit( :telephone_number).merge(user_id: current_user.id, recruitment_id: params[:recruitment_id], token: params[:token]) end def pay_recruitment Payjp.api_key = ENV["PAYJP_SECRET_KEY"] # PAY.JPテスト秘密鍵 Payjp::Charge.create( amount: @recruitment.order, # 商品の値段 card: order_params[:token], # カードトークン currency: 'jpy' # 通貨の種類(日本円) ) end end
モデル
class Recruitment < ApplicationRecord has_one :record belongs_to :user has_one_attached :image extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :category validates :heading, :content, :price, :image, :category_id, presence: true validates :category_id, numericality: { other_than: 1, message: "を入力してください"} validates :price, numericality: {only_integer: true, greater_than_or_equal_to: 500, less_than_or_equal_to: 999999, message: "を¥500〜999,999に訂正してください"} end
class Record < ApplicationRecord belongs_to :user belongs_to :recruitment has_one :order end
class Order < ApplicationRecord include ActiveModel::Model attr_accessor :telephone_number, :user_id, :item_id, :token with_options presence: true do validates :token validates :telephone_number, format:{with: /\A\d{10,11}\z/ } validates :user_id validates :recruitment_id end def save record = Record.create(recruitment_id: recruitment_id, user_id: user_id) Address.create(telephone_number: telephone_number, record_id: record.id) end end
マイグレーション
orders class CreateOrders < ActiveRecord::Migration[6.0] def change create_table :orders do |t| t.string :telephone_number, null: false t.references :record, null: false, foreign_key: true t.timestamps end end end
class CreateRecords < ActiveRecord::Migration[6.0] def change create_table :records do |t| t.references :user, null: false, foreign_key: true t.references :recruitment, null: false, foreign_key: true t.timestamps end end end
試したこと
マイグレーションの確認。
データベースにはorderにはrecruitment_idが入っています。
補足情報(FW/ツールのバージョンなど)
railsのバージョン
Rails 6.0.4.1
gemのバージョン
3.0.3
追記・修正があればよろしくお願いいたします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/29 10:45