0

我有 3 张桌子OwnerofProperty-PropertyTicket. 我想制作一个表格form_for来代表物业预订;我可以制作一个表单来从Property提交按钮将数据保存在Ticket表中的位置检索数据吗?我问是因为我不知道这是否可能或如何做到。

注意:我只创建了关系: OwnerofProperty一对多Property

Property 一对一 Ticket

我需要这个表格只是为了让用户能够看到可用的属性并且只能预订一个,如何制作这个表格?

三种模型的 Schema.rb :

create_table "owners", :force => true do |t|
    t.string   "f_name"
    t.string   "l_name"
    t.string   "address"
    t.string   "tel_no"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "properties", :force => true do |t|
    t.string   "p_street"
    t.string   "p_city"
    t.string   "postcode"
    t.string   "property_type"
    t.integer  "rooms"
    t.integer  "rent"
    t.integer  "owner_id"
    t.datetime "created_at",    :null => false
    t.datetime "updated_at",    :null => false
  end

  add_index "properties", ["owner_id"], :name => "index_properties_on_owner_id"

  create_table "tickets", :force => true do |t|
    t.string   "city"
    t.string   "street"
    t.string   "ticket_type"
    t.integer  "rooms"
    t.integer  "rent"
    t.integer  "property_id"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  add_index "tickets", ["property_id"], :name => "index_tickets_on_property_id"
4

1 回答 1

0

对的,这是可能的。

让我们来看看ticket_controller.rb

def new
  @property = Property.find 20  #20 is property id
  @properties = Property.all
  #@ticket = Ticket.new
end

现在在view(您要创建表单的地方):

<%= form_for @ticket do |f| %>
<%= f.select :property_id, @properties.collect {|p| [ p.name, p.id ] }%> <!-- just an example, Ticket model has a field named "property_id" -->
<%= f.submit %>
<%= end %>

此表单提交给create的操作ticket_controller。您可以获取所有数据params并将其保存到表格中。

def create
@ticket = Ticket.new(params[:ticket])
@ticket.save
  respond_to do |format|
    format.html{redirect_to( your_desired_path)}
  end
end
于 2013-05-03T10:07:23.400 回答