【发布时间】:2013-06-21 15:09:26
【问题描述】:
我有两个模型:
class GarageOwner < ActiveRecord::Base
has_many :garages, dependent: :destroy
end
class Garage < ActiveRecord::Base
belongs_to :garage_owner
end
如果没有车库所有者,车库永远不应该存在。所以在GaragesController 的new 动作中,我需要相应的车库所有者。我不想使用嵌套路由,所以我没有车库所有者 ID 作为参数。但是那我怎么弄到他呢?
更新说明
车库由第三个模型 (Admin) 创建。所以我无法通过当前用户访问车库所有者。
我使用resources 构建我的路线:
garage_owners GET /garage_owners(.:format) garage_owners#index
POST /garage_owners(.:format) garage_owners#create
new_garage_owner GET /garage_owners/new(.:format) garage_owners#new
edit_garage_owner GET /garage_owners/:id/edit(.:format) garage_owners#edit
garage_owner GET /garage_owners/:id(.:format) garage_owners#show
PUT /garage_owners/:id(.:format) garage_owners#update
DELETE /garage_owners/:id(.:format) garage_owners#destroy
garages GET /garages(.:format) garages#index
POST /garages(.:format) garages#create
new_garage GET /garages/new(.:format) garages#new
edit_garage GET /garages/:id/edit(.:format) garages#edit
garage GET /garages/:id(.:format) garages#show
PUT /garages/:id(.:format) garages#update
DELETE /garages/:id(.:format) garages#destroy
【问题讨论】:
-
您应该将其作为参数传递。你的路线看起来如何?顺便说一句,您为什么不想使用嵌套路由?
-
我不想让“garage_owner”出现在我的网址中。我更新了问题。你可以在那里看到路线。
-
garage_owner从何而来?它不在 URL 中,与关联用户无关,任何人或应用程序如何知道garage_owner是哪个?
标签: ruby-on-rails ruby-on-rails-3.2