hey am new to rails, i have homes table , users , and payments. I want something like this, when a user creates payments , it should contain user_id and home_id, i have managed to get user_id working but home_id failing.
`i have looked at many solutions here on stack overflow but i have not found the solution. I could be having problems with this line of code
home = Home.find(params[:home_id])
where i wanted to find home with an id the i use it while creating payments`
payment controller
class Api::V1::PaymentsController < ApplicationController
before_action :set_payment, only: %i[ show edit update destroy ]
before_action :authenticate_user!
# GET /payments or /payments.json
def index
if user_signed_in?
@payments = current_user.payments.order(created_at: :desc)
render json: @payments
else
render json: {}, status: 401
end
end
# GET /payments/1 or /payments/1.json
def show
if @payment
render json: @payment
else
render json:@payment.errors
end
end
# GET /payments/new
def new
payment = current_user.payments.new
# home = Home.find(params[:home_id])
end
# GET /payments/1/edit
def edit
end
# POST /payments or /payments.json
def create
home = Home.find(params[:home_id])
if user_signed_in?
payment = home.payments.create(payment_params.merge(user_id: current_user.id))
if payment = current_user.payments.create(payment_params, home)
render json: payment, status: :created
else
render json: payment.errors, status: 400
end
else
render json: {}, status: 401
end
end
# PATCH/PUT /payments/1 or /payments/1.json
def update
if @payment.update(payment_params)
render json: {notice: "Payment was successfully updated." }
else
render json: { error: 'Unable to update payment' }
end
end
# DELETE /payments/1 or /payments/1.json
def destroy
@payment.destroy
render json: {notice: 'Payment succefully removed'}
end
private
# Use callbacks to share common setup or constraints between actions.
def set_payment
@payment = Payment.find(params[:id])
end
# Only allow a list of trusted parameters through.
def payment_params
params.require(:payment).permit(:first_name, :last_name, :phone_number, :address, :money_paid, :date, :nin_number, :user_id, :home_id)
end
end
schema
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_12_15_071948) do
create_table "homes", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "image_url"
t.decimal "price", precision: 8, scale: 2
t.string "availability", default: "Available", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "payments", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "phone_number"
t.text "address"
t.decimal "money_paid"
t.string "nin_number"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "home_id"
t.date "date"
t.integer "user_id"
t.index ["home_id"], name: "index_payments_on_home_id"
t.index ["user_id"], name: "index_payments_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "payments", "homes"
add_foreign_key "payments", "users"
end
home.rb
class Home < ApplicationRecord
validates :title, :description, :image_url, :price, :availability, presence: true
validates :price, numericality: { greater_than_or_equal_to: 0.01 }
validates :image_url, allow_blank: true, format: {
with:
%r{.(gif|jpg|png|jpeg)z}i,
message: 'must be a URL for GIF, JPG, JPEG or PNG image.'
}
has_many :payments
end
payment.rb
class Payment < ApplicationRecord
validates :first_name, :last_name, :phone_number, :address, :money_paid, :date, :nin_number, :user_id, presence: true
belongs_to :user
belongs_to :home
end
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :payments
end
anyone with any idea of how i can go about it. i want payments to include user_id and home_id which the user is paying for.
here are my routes if it could be a routing error
Rails.application.routes.draw do
devise_for :users
namespace :api do
namespace :v1 do
get 'homes/index'
post 'homes/create'
get '/show/:id', to: 'homes#show'
delete '/destroy/:id', to: 'homes#destroy'
# resources :payments
get '/payments/index'
post '/payments/create'
end
end
root 'homes#index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end