嗨,这是我第一次使用嵌套资源。我做了rake routes
并找到了new_user_album_path的路径,但它似乎不起作用。问题可能是因为我做了双巢吗?
当我单击名为show.html.erb的用户视图文件中的链接时,问题就出现了。试图链接到“创建相册”页面,但是当它显示错误时:
No route matches {:action=>"new", :controller=>"albums"}
以下是我的文件:
show.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
答案 0 :(得分:1)
您需要将用户传递给路线方法,例如:
new_user_album_path(@user)