我有一個應用程序,允許用戶創建加載和出價。在rails中創建預訂
我想要一種方式來瀏覽應用程序並保留出價,然後處理信用卡,但我在設置預訂模型時遇到了問題。下面是設置:
模型與他們的關聯:
class Reservation < ActiveRecord::Base
belongs_to :load
belongs_to :bid
validates :load_id, presence: true
validates :bid_id, presence: true
end
class Load < ActiveRecord::Base
has_many :bids, :dependent => :delete_all
has_one :reservation
end
class Bid < ActiveRecord::Base
belongs_to :load
has_one :reservation
end
預約遷移只包括這兩個表的引用。 在我的預訂控制器我有以下代碼:
class ReservationsController < ApplicationController
before_filter :find_load
before_filter :find_bid
def new
@reservation = @load.build_reservation(params[:reservation])
end
def create
@reservation = @load.build_reservation(params[:reservation].merge!(:bid_id => params[:bid_id]))
if @reservation.save
flash[:notice] = "Reservation has been created successfully"
redirect_to [@load, @bid]
else
render :new
end
end
def find_load
@load = Load.find(params[:load_id])
end
def find_bid
@bid = Bid.find(params[:bid_id])
end
end
在我的配置routes文件我有以下幾點:
resources :loads do
resources :bids do
resources :reservations
end
end
爲保留模型的遷移是這樣的:
def change
create_table :reservations do |t|
t.references :load
t.references :bid
t.timestamps
end
end
該查看號碼:
<h4>Reserve this bid: </h4>
<dl>
<dt>Carrier</dt>
<dd><%= @bid.user.email %></dd>
<dt>Bid Amount:</dt>
<dd><%= @bid.bid_price %></dd>
</dl>
<%= form_for [@load, @bid, @reservation], :html => {:class => 'payment_form'} do |f| %>
<%= f.error_messages %>
<fieldset>
<div class="field">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_code, "Security Code on Back of Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, { add_month_numbers: true }, { name: nil, id: "card_month" } %>
<%= select_year nil, { start_year: Date.today.year, end_year: Date.today.year+15 },
{ name: nil, id: "card_year" } %>
</div>
</fieldset>
當我提交表單時,出現以下驗證錯誤:「出價不能爲空」。它看起來像表格提交的出價爲nil
,我不確定它爲什麼這樣做。我不相信第一行代碼在我的控制器的創建動作中是正確的,並且我嘗試了所有我能想到的排列方式,但我無法實現它。
哪條線給你一個錯誤?很可能它無法通過'load_id'找到負載 – Zepplock
預約控制器; @reservation = load.reservation.build它可以找到負載和出價,因爲參數散列值都有。我不知道爲什麼它不起作用。 – tomciopp
顯然你的'預訂'是'nil',請確定你已經在你的數據庫中設置了 – Zepplock