Rails:如何链接到双嵌套资源,使其属于前2级?

我正在尝试制作一个画册网络应用程序。 有3种型号:用户,专辑,图片。

配置/路线

Pholder::Application.routes.draw do resources :users do resources :albums do resources :photos end end 

我设法在用户下制作用户和制作专辑,正如您在专辑#show的URL中看到的那样:

http://localhost:3000/users/20/albums/94 (没问题)

但是,在那个页面上,我想创建一个链接,在一个专辑下创建照片,这个专辑都在一个用户之下(一个URL看起来像/ users / 20 / albums / 94 / photos / new的东西),然后跟着我的rake routes我有一个new_album_photo路径。 这是我的专辑/节目视图

show.html.erb

  yes pics  no pics   

但是当我点击它时,会出现两个问题。

  1. 我收到错误: No route matches [GET] "/albums/94/photos/new"
  2. URL中没有user_id … http://localhost:3000/albums/94/photos/new 。 我的模型可能是造成这种情况的原因(因为根据我的模型,专辑是由多个用户拥有的吗?我不确定是否应该删除连接表并且只将Album模型作为连接表(可能连接表是罪魁祸首?)

这可能是因为我的模特? 或者我在路线助手中传递了错误的参数?

这是我的模型以防万一。 楷模:

 class User  :create validates_format_of :name, :with => /[A-Za-z]+/, :on => :create validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[az]{2,})\Z/i, :on => :create validates_length_of :password, :minimum => 5, :on => :create has_many :album_user has_many :albums, :through => :album_user accepts_nested_attributes_for :albums end class AlbumUser < ActiveRecord::Base belongs_to :album belongs_to :user end class Album  :album_user has_many :photos end class Photo < ActiveRecord::Base belongs_to :album end 

路线

  user_albums GET /users/:user_id/albums(.:format) albums#index POST /users/:user_id/albums(.:format) albums#create new_user_album GET /users/:user_id/albums/new(.:format) albums#new edit_user_album GET /users/:user_id/albums/:id/edit(.:format) albums#edit user_album GET /users/:user_id/albums/:id(.:format) albums#show PUT /users/:user_id/albums/:id(.:format) albums#update DELETE /users/:user_id/albums/:id(.:format) albums#destroy users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy album_photos GET /albums/:album_id/photos(.:format) photos#index POST /albums/:album_id/photos(.:format) photos#create new_album_photo GET /albums/:album_id/photos/new(.:format) photos#new edit_album_photo GET /albums/:album_id/photos/:id/edit(.:format) photos#edit album_photo GET /albums/:album_id/photos/:id(.:format) photos#show PUT /albums/:album_id/photos/:id(.:format) photos#update DELETE /albums/:album_id/photos/:id(.:format) photos#destroy albums GET /albums(.:format) albums#index POST /albums(.:format) albums#create new_album GET /albums/new(.:format) albums#new edit_album GET /albums/:id/edit(.:format) albums#edit album GET /albums/:id(.:format) albums#show PUT /albums/:id(.:format) albums#update DELETE /albums/:id(.:format) albums#destroy root / users#index about /about(.:format) home#about help /help(.:format) home#help contact /contact(.:format) home#contact 

如果您需要更多文件,请告诉我。

你的routes.rb文件应该是这样的(额外的end ):

 Pholder::Application.routes.draw do resources :users do resources :albums do resources :photos end end end 

album.rb应该是这样的:album_users:album_users not :album_user ):

 class Album < ActiveRecord::Base has_many :album_users has_many :users, :through => :album_users has_many :photos end 

user.rb应该是这样的( :album_users not :album_user ),你可以这样添加照片关系:

 class User < ActiveRecord::Base has_many :album_users has_many :albums, :through => :album_users has_many :photos, :finder_sql => proc {"select * from photos inner join albums on albums.id = photos.album_id inner join album_users on album_users.album_id = albums.id where album_users.user_id = #{id}"} accepts_nested_attributes_for :albums end 

你应该在routes.rb看到new_user_album_photo并像这样使用它:

 new_user_album_photo_path(@user, @album) 

您可以在控制台中测试您的路线,如下所示:

 app.new_user_album_photo_path(User.first, Album.first) 

它应该返回:

 => "/users/1/albums/1/photos/new"