我有一个列出所有广告的索引页面(简单的脚手架生成列表)。我已经这样做,当我点击广告时,它将展开(css和jquery)并显示该广告的信息。现在我无法解决的问题是,我希望能够通过索引ads_item视图中的表单预订该广告。
并且要清楚,以便我们在同一页面上是一个关于我正在做什么的模型
我得到的错误是
param is missing or the value is empty: ads_item
这就是我得到的
路由
resources :ads_items do
resources :bookings
end
ads_items控制器
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
预订控制器
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
模型ads_item
class AdsItem < ActiveRecord::Base
belongs_to :user
has_many :bookings
accepts_nested_attributes_for :bookings
end
预订模型
class Booking < ActiveRecord::Base
belongs_to :ads_item
end
数据库
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
和ads_items索引视图在哪里列出所有项目(脚手架)
<% @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 %>
我确实遵循了此Rails guide
但它不会奏效。我在这里错过了什么吗?如何在填写表单后呈现表单以便获取广告ID并将其保存到预订表中?
答案 0 :(得分:0)
到目前为止你所做的一切看起来是正确的。您可以使用rails中的嵌套属性功能通过AdItem模型创建预订,请查看此处的文档http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
您可以使用视图中的fields_for方法呈现预订表单。