ActiveScaffold—当插入新记录时,如何启用正在创建的模型“属于”的列/模型的输入字段。
我有以下数据库架构:
create_table "addresses", :force => true do |t|
t.string "road"
t.string "city"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "client_id"
end
create_table "clients", :force => true do |t|
t.integer "address_id"
t.integer "order_id"
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "orders", :force => true do |t|
t.integer "order_id"
t.integer "client_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
和模型:
class Client < ActiveRecord::Base
belongs_to :address
end
class Order < ActiveRecord::Base
belongs_to :client
end
class Address < ActiveRecord::Base
end
此设置的目的是记录许多客户端,每个客户端都有一个地址。多个客户端可以具有相同的地址。地址表中的 client_id 用于此目的。
当我访问 /Clients ActiveScaffold 视图并单击“创建”时,我可以输入新客户端的数据,包括客户端新地址的数据。
但是,当我访问/Orders视图并单击创建时,我可以添加一个新客户并为他输入数据,但是对于地址只有一个选择框,只能用于选择现有地址,有没有字段可以为新客户创建新地址。 如何包含新客户的地址字段,以便为客户创建新地址?
提前致谢
I have the following database schema:
create_table "addresses", :force => true do |t|
t.string "road"
t.string "city"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "client_id"
end
create_table "clients", :force => true do |t|
t.integer "address_id"
t.integer "order_id"
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "orders", :force => true do |t|
t.integer "order_id"
t.integer "client_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
and models:
class Client < ActiveRecord::Base
belongs_to :address
end
class Order < ActiveRecord::Base
belongs_to :client
end
class Address < ActiveRecord::Base
end
The intention of this setup is to have records of many Clients, each Client has an address. Multiple clients can have the same address. The client_id in the addresses table is used for this purpose.
When I visit the /Clients ActiveScaffold view, and click create I am able to enter data for the new client, including the data of the new address for the client.
But, when I visit the /Orders view and click create, I can add a new Client and enter the data for him, but for the address there is only a select box, which only can be used to select an existing address, there are no fields to create a new address for the new client.
How can I include the address fields for the new client, in order to create a new address for the client?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并没有真正将 ActiveScaffold 推到那么远,但是表上的关联看起来有点奇怪 - 关系的双方都有一个外键,即:
并且
我认为只有其中的一侧是严格需要的,例如地址上的client_id 。
可能相关,但在关系的任一侧都有belongs_to - 也许一侧应该是 has_one 关系,即:
希望有帮助,克里斯。
Not really push ActiveScaffold that far, but the associations on your tables look a little strange - you have a foreign key on both sides of the relationship, that is:
and
I'd have thought only one side of that is strictly needed, eg client_id on addresses.
Possibly related, but you have belongs_to on either side of the relationship - perhaps one side should be a has_one relationship, that is:
Hope that helps, Chris.