2012-01-17 92 views
1

因此,我正在开发一个新的rails项目,它涉及从单个rails安装中运行多个网站。我对如何在不嵌套资源的情况下进行关联感到困惑。Rails 3多站点(与id的关联)?

注:我使用的子域网址这样会site1.ex.com/photos, site2.ex.com/ramps

比如现在我有一个网站叫网站1网站1有照片,视频和坡道。我可以通过告诉rails site has_many来做这个关联:photos,:videos,:ramps(代码没有正确格式化,只是给了你这个想法)。一切工作正常,包括网址,但这是我的问题。

  1. 网站是包含在我不想要的网址(例如example.com/sites/1/photos)。
  2. 协会工作正常,但如果我想为这3件物品的父母呢? Rails建议你不要在1层以上嵌套路由。 enter image description here

这里是我想要做的 enter image description here

  1. 我怎样才能叫网站之间的关联,而无需使用嵌套资源(不能捕捉album_id照片的SITE_ID)是什么?
  2. 如果专辑has_many:照片belongs_to:site我该如何在我的控制器(任何示例)中调用?

因为我添加了照片,所以有人可以理解我想要完成什么。

回答

1

如果您使用子域名,则不必为sites使用嵌套资源。要找到一个网站:

class ApplicationController < ActionController::Base 

    before_filter :find_site_by_subdomain 

    def find_site_by_subdomain 
    @site = Site.find_by_subdomain!(request.subdomain) 
    end 

end 

对于所有其他资源,你巢他们不是在Site而是以自己为父母(相册/照片,视频,坡道)。而在你AlbumsController你只想要那些属于当前子域名的网站,让你利用你的before_filter从ApplicationController和使用@site对象:

class AlbumsController < ApplicationController 

    … 

    def show 
    @album = @site.albums.find(params[:id]) 
    end 

    … 
end 
+0

如果什么专辑'的has_many:photos'如何我可以将专辑,网站和照片集成到一个电话或我的控制器中吗? – coletrain 2012-01-17 16:00:39

+1

制作一个PhotosController,添加'before_filter:find_album'并制作一个名为'find_album'的方法,在其中调用'@album = @ site.albums.find(params [:album_id])''。然后在你的show方法中,你可以调用'@photo = @ album.photos.find(params [:id])'' – 2012-01-17 19:39:32