Ruby on rails Rails 3嵌套资源与否?
我有一家“公司”,里面有“项目”。随着时间的推移,我们现在需要添加与“项目”相关的“链接”。我的路线目前如下所示:Ruby on rails Rails 3嵌套资源与否?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一家“公司”,里面有“项目”。随着时间的推移,我们现在需要添加与“项目”相关的“链接”。我的路线目前如下所示: resources :companies do resources :projects do resources :links end end 这似乎是错误的,因为嵌套深度为2层。如果我这样嵌套,我也不再有新的公司项目路径(@company),这使得我无法为公司创建项目 我将需要添加其他模型,以在未来几个月与项目相关 这是我的项目模型和链接模型 class Lin
resources :companies do
resources :projects do
resources :links
end
end
这似乎是错误的,因为嵌套深度为2层。如果我这样嵌套,我也不再有新的公司项目路径(@company),这使得我无法为公司创建项目
我将需要添加其他模型,以在未来几个月与项目相关
这是我的项目模型和链接模型
class Link < ActiveRecord::Base
attr_accessible :link_name, :url, :description
belongs_to :project
end
class Project < ActiveRecord::Base
belongs_to :company
belongs_to :user
validates :title, :presence => true
validates :description, :presence => true,
:length => { :minimum => 10 }
end
class链接true
验证:description,:presence=>true,
:length=>{:minimum=>10}
结束
筑巢似乎不是正确的方式。如果嵌套不是正确的方法,如何保存关联?例如,在当前控制器中,我通过以下操作保存嵌套对象:
class ProjectsController < ApplicationController
before_filter :authenticate_user!
before_filter :find_company
def new
@project = @company.projects.build
end
def create
@project = @company.projects.build(params[:project])
if @project.save
flash[:notice] = "Project has been created."
redirect_to [@company, @project]
else
flash[:alert] = "Project has not been created."
render :action => "new"
end
end
private
def find_company
@company = Company.find(params[:company_id])
end
end
class ProjectsController“新建”
结束
结束
私有的
def find_公司
@company=company.find(参数[:company\u id])
结束
结束
我找不到太多关于这个主题的信息,我以前读过的书使用的筑巢路线只有1层深,其他的根本没有筑巢
那么,最好的方法是什么,这样我就可以拥有与“项目”相关的“链接”和其他模型,而“项目”仍然与“公司”相关?嵌套路由和“嵌套”模型是两件不同的事情
以现在的方式嵌套模型似乎还可以
至于路由,考虑让它们<代码>浅< /C> >,如解释和
,您可以用这样的浅嵌套路由来处理:resources :companies do
resources :projects
end
resources :projects do
resources :links
resources :sausages
resources :patties
end
然后你就有了新公司、新项目、新链接等路线