Hi I’m currently working with nested resources.
routes
Pholder::Application.routes.draw do
resources :users do
resources :albums do
resources :photos
end
end
end
I have 3 models (users, albums, photos). I’ve managed to register users and create albums, but am stuck on trying to make the form for photos. On creation of an album, the user is redirected to the albums/show page:
album/show
<% if @album.photos.any? %>
yes pics
<% else %>
no pics
<% end %>
<%= link_to "Upload new pics!", new_user_album_photo_path(@user, @album) %>
as you can see, on the bottom of the page there’s a path to new photos, and that’s where the issue is. When I click on the link, it gives me an error:
undefined methodphotos_path’ for #<#:0x007fb69e167220>`
the error occurs on line #3 of that page (photos/new)
photos/new
<% provide(:title, "Upload pictures") %>
<%= form_for(@photo, :html => { :multipart => true }) do |f| %>
<%= f.file_field :photo %>
<% end %>
I suspect that I’m putting the wrong information to the controller (I’m still very shaky on what to put in the controller.)? Here’s my photos controller.
photos controller
class PhotosController < ApplicationController
def new
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:album_id])
@photo = @album.photos.build
end
def create
@album = Album.find(params[:album_id])
@photo = @album.photos.build(params[:photo])
respond_to do |format|
if @album.save
format.html { redirect_to @album, notice: 'Album was successfully created.' }
format.json { render json: @album, status: :created, location: @album}
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
def show
@album = Album.find(params[:album_id])
@photos = @album.photos
end
end
Is my form incorrect? It’s confusing to me what to put in the controller and whether the error is occurring in the form or the controller. Thanks
let me know if you need any more info.
The same way you have to provide parent resources to the
link_tohelper, you also have to supply it to the form. So change the form line to this:.. and it should work!