在处理此应用程序的投标时,我需要一些帮助。当用户注册时,他们可以设置预算(架构中的整数)。然后,用户可以创建具有固定价格的商品(这也是商品表架构中的整数)。
我试图弄清楚用户何时为该项目创建出价
A)如果出价最高的人,我如何减少用户当前的预算,但如果出价,钱会回到他们的预算。
B) 提高已设置为当前最高出价的项目“价格”。
这是架构的视觉效果:
create_table "bids", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "item_id"
t.integer "user_id"
t.integer "amount"
end
create_table "items", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "title"
t.integer "price"
end
create_table "users", force: true 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.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "budget"
end
投标控制器:
class BidsController < ApplicationController
before_action :set_bid, only: [:show, :edit, :update, :destroy]
def show
@bid = Bid.find(params[:id])
end
def new
@item = Item.find(params[:item_id])
@bid = @item.bids.build
end
def create
@item = Item.find(params[:item_id])
@bid = @item.bids.new(bid_params)
respond_to do |format|
if @bid.save
format.html { redirect_to @item, notice: 'Your item has been updated.'}
else
format.html { render action: 'new' }
end
end
end
private
def set_bid
@bid = Bid.find(params[:id])
end
def bid_params
params[:bid][:user_id] = current_user.id
params[:bid].permit(:amount, :user_id, :item_id)
end
end
物品控制器:
class ItemsController < ApplicationController
before_action :set_item, only: [:show, :edit, :update, :destroy]
before_filter :user_signed_in?
def index
@item = Item.all
end
def show
@item = Item.find(params[:id])
end
def edit
end
def new
@item = Item.new
@item.bids.build
end
def create
@item = Item.new(item_params)
if @item.save
redirect_to @item, notice: 'Item successfully created.'
else
format.html { render action: 'new'}
end
end
def update
respond_to do |format|
if @item.update(item_params)
format.html { redirect_to @item, notice: 'Your item has been updated.'}
else
format.html { render action: 'edit'}
end
end
end
def destroy
@item.destroy
respond_to do |format|
format.html { redirect_to items_url }
end
end
private
def set_item
@item = Item.find(params[:id])
end
def item_params
params[:item][:user_id] = current_user.id
params[:item].permit(:price, :user_id, :title, :bids_attributes => [:amount, :user_id, :item_id])
end
end