bigpotato
bigpotato

Reputation: 27507

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. 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 . 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

<% 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

Upvotes: 0

Views: 75

Answers (1)

numbers1311407
numbers1311407

Reputation: 34072

You need to pass the user to the route method, like:

new_user_album_path(@user)

Upvotes: 1

Related Questions