简体   繁体   English

Rails中的嵌套资源没有路由错误

[英]No route error for nested resources in Rails

Hi this is my first time using nested resources. 嗨,这是我第一次使用嵌套资源。 I did rake routes and found a path to new_user_album_path, but it doesn't seem to work. 我做了rake routes并找到了指向new_user_album_path的路径,但是它似乎不起作用。 Could the problem be because I did a double nest? 问题可能是因为我做了双巢吗?

The problem is specifically when I click a link in my Users views file named show.html.erb . 特别是当我单击“用户”视图文件中名为show.html.erb的链接时,问题就出现了。 Trying to link to a "create an album" page but that's when it shows me an error: 尝试链接到“创建相册”页面,但这是当它向我显示错误时:

No route matches {:action=>"new", :controller=>"albums"}

Here are my files: 这是我的文件:

show.html.erb 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 %>

Config/routes 配置/路由

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

Controller 控制者

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

您需要将用户传递给route方法,例如:

new_user_album_path(@user)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM