bigpotato
bigpotato

Reputation: 27527

Rails: undefined method `photos_path'?

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.

Upvotes: 3

Views: 570

Answers (2)

jonkgrimes
jonkgrimes

Reputation: 86

So what's happening here is Rails is using a bit of reflection looking at the object you passed to the form helper, which (I'm guessing) is simply a Photo object. Rails, by default, will look for photos_path as that is where the POST request will typically go for the create action. So it's posting to /photos which, unfortunately doesn't exist based on your current routes.

If you change that form helper line to:

<%= form_for [@user,@album,@photo], html: { multipart: true} do |f| %>

Which causes it to post to /users/(user id)/albums/(album id)/photos which should create the new photo.

Upvotes: 2

Peter Duijnstee
Peter Duijnstee

Reputation: 3779

The same way you have to provide parent resources to the link_to helper, you also have to supply it to the form. So change the form line to this:

 <%= form_for([@user, @album, @photo], :html => { :multipart => true }) do |f| %>

.. and it should work!

Upvotes: 5

Related Questions