I have a index page(simple scaffold generated listings) that list all ads. I have made so that when I click on a ad it will expand(css and jquery) and show the information on that ad. Now the problem that I cant get to work is that I want to be able to book that ad through a form inside of the index ads_item views.
and to get clear so that we are on the same page here is a mockup on what I am doing
The error i get is
param is missing or the value is empty: ads_item
This is what I got
routes
resources :ads_items do
resources :bookings
end
ads_items controller
def new
@ads_item = AdsItem.new
end
def create
@ads_item = AdsItem.new(ads_item_params)
respond_to do |format|
if @ads_item.save
format.html { redirect_to @ads_item, notice: 'Ads item was successfully created.' }
format.json { render :show, status: :created, location: @ads_item }
else
format.html { render :new }
format.json { render json: @ads_item.errors, status: :unprocessable_entity }
end
end
end
bookings controller
def new
@booking = Booking.new
end
def create
@booking = Booking.new(booking_params)
respond_to do |format|
if @booking.save
format.html { redirect_to @booking, notice: 'Booking was successfully created.' }
format.json { render :show, status: :created, location: @booking }
else
format.html { render :new }
format.json { render json: @booking.errors, status: :unprocessable_entity }
end
end
end
Model ads_item
class AdsItem < ActiveRecord::Base
belongs_to :user
has_many :bookings
accepts_nested_attributes_for :bookings
end
Model booking
class Booking < ActiveRecord::Base
belongs_to :ads_item
end
database
create_table "ads_items", force: true do |t|
t.integer "user_id"
t.date "date"
t.time "time"
t.string "type_of_service"
t.string "address"
t.text "include_in_price"
t.integer "rate"
t.integer "original_price"
t.integer "now_price"
t.boolean "booked"
t.string "category"
t.datetime "created_at"
t.datetime "updated_at"
t.string "company_information"
t.string "company_website"
t.string "company_facebook"
t.string "company_twitter"
t.string "company_instagram"
t.string "company_google_maps"
t.string "company_phone"
t.string "company_name"
t.string "logo_path"
end
create_table "bookings", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "social_nr"
t.string "phone_nr"
t.string "email"
t.integer "ads_item_id"
t.datetime "created_at"
t.datetime "updated_at"
end
and the ads_items index view where is list all the items (scaffolded)
<% @ads_items.each do |ads_item| %>
list all items
<%= form_for :booking do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.text_field :social_nr %>
<%= f.text_field :phone_nr %>
<%= f.text_field :email %>
<%= f.number_field :ads_item_id, :value => ads_item.id %>
<%= f.submit %>
<% end %>
<% end %>
I did follow this Rails guide
but it won't work. Am I missing something here ? how do I render the form so that it will get the ads id and save it to the booking table after I filled out the form ?
What you have done so far looks correct. You can use the nested attributes feature in rails to create a booking through the AdItem model, have a look at the documentation here http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
You would use the fields_for method in your view to render the booking form.