嗨,这是我第一次使用嵌套资源。我确实rake routes
找到了指向 new_user_album_path 的路径,但它似乎不起作用。问题可能是因为我做了双窝吗?
问题出在我单击用户视图文件中名为 show.html.erb 的链接时。尝试链接到“创建相册”页面,但此时它向我显示错误:
No route matches {:action=>"new", :controller=>"albums"}
这是我的文件:
显示.html.erb
<% provide(:title, "Welcome, #{@user.name}!") %>
<div>
You currently have <%= pluralize(@user.albums.count, "album") %>
</div>
<div>
<%= link_to "Create a new album!", new_user_album_path %>
</div>
<div>
<% if @user.albums.any? %>
hey
<% else %>
boo
<% end %>
</div>
<%= link_to "Back", users_path %>
配置/路由
Pholder::Application.routes.draw do
resources :users do
resources :albums do
resources :pictures
end
end
控制器
class AlbumsController < ApplicationController
def index
@albums = Albums.all
respond_to do |format|
format.html
format.json { render json: @albums }
end
end
def new
@user = User.find(params[:user_id])
end
end