2012-05-10 78 views
1

我有一個應用程序,允許用戶創建加載和出價。在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,我不確定它爲什麼這樣做。我不相信第一行代碼在我的控制器的創建動作中是正確的,並且我嘗試了所有我能想到的排列方式,但我無法實現它。

+0

哪條線給你一個錯誤?很可能它無法通過'load_id'找到負載 – Zepplock

+0

預約控制器; @reservation = load.reservation.build它可以找到負載和出價,因爲參數散列值都有。我不知道爲什麼它不起作用。 – tomciopp

+0

顯然你的'預訂'是'nil',請確定你已經在你的數據庫中設置了 – Zepplock

回答

1

因爲你的窗體不包含你會節省你可以簡單地做到這一點的任何數據:

@reservation = @load.build_reservation(:bid_id => @bid.id) 

如果您添加要在將來節省預訂領域,params[:reservation]不會nil你將能夠做到:

@reservation = @load.build_reservation(params[:reservation].merge(:bid_id => @bid.id)) 

另一種方法是將一個隱藏字段添加到您的表單包含bid_id,但既然你已經擁有它的URL,這可能不是最好的方法。

+0

你是正確的,它應該是params [:reservation],但是當我將合併方法添加到params [:reservation]時,我得到了「undefined method merge」。爲零:NilClass「 – tomciopp

+0

嗯,顯然'參數[:保留]'是'無'。你可以嘗試調試你的'params'哈希,並在提交表單後看看裏面有什麼? – Mischa

+0

嗯這是奇怪params哈希表看起來像這樣:{ 「UTF8」=> 「✓」, 「authenticity_token」=> 「AVkc4PWygoH4OZUlQl5Z1g3KCirZ + FcW03kmNcwXx0M =」, 「load_id」=> 「37」, 「bid_id」= > 「20」};這讓我非常困惑,因爲bid_id在那裏,而且我完全無法獲得params散列的值 – tomciopp