I have an application that allows users to create loads and bid on them.
I want a way to go through the application and reserve a bid which will then process a credit card, but I am having issues setting up the reservation model. Here is the setup:
Models with their associations:
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
The reservation migration only includes the references to the two tables.
In my reservations controller I have the following code:
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
In my config routes file I have the following:
resources :loads do
resources :bids do
resources :reservations
end
end
The migration for the reservations model looks like this:
def change
create_table :reservations do |t|
t.references :load
t.references :bid
t.timestamps
end
end
The View Code:
<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>
When I go to submit the form, I get the following validation error: “Bid can’t be blank”. It looks like the form is submitting the bid as nil, and I am unsure of why it is doing this. I don’t believe the first line of code is correct in my create action of the controller, and I have tried all of the permutations that I could think of and I can’t get it to work.
Since your form doesn’t contain any data you’ll be saving you can simply do this:
If you add reservation fields that you want to save in the future,
params[:reservation]will not beniland you will be able to do:Another way would be to add a hidden field to your form that contains the
bid_id, but since you already have it in the URL, this may not be the best approach.