我正在尝试制作相册网络应用程序。有 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
路。这是我的相册/显示视图
显示.html.erb
<% if @album.photos.any? %>
yes pics
<% else %>
no pics
<% end %>
<%= link_to "Upload new pics!", new_album_photo_path(@album) %>
但是,当我单击它时,会出现两个问题。
- 我收到错误:
No route matches [GET] "/albums/94/photos/new"
- 该 URL 中没有 user_id
http://localhost:3000/albums/94/photos/new
...... 我的模型可能是造成这种情况的原因吗(因为根据我的模型,专辑由多个用户拥有?我不确定是否应该删除连接表,只让Album
模型成为连接表(也许连接表是罪魁祸首?)
这可能是因为我的模型吗?还是我在路由助手中传递了错误的参数?
这是我的模型以防万一。楷模:
class User < ActiveRecord::Base
has_secure_password
attr_accessible :email, :name, :password, :password_confirmation
validates_presence_of :password, :on => :create
validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{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 < ActiveRecord::Base
attr_accessible :avatar, :name, :description
has_many :album_user
has_many :users, :through => :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
如果您需要更多文件,请告诉我。