undefined method path with simple form
Asked Answered
M

2

9

I have albums and pictures where albums hasmany pictures

This is my routes.rb

 resources :albums do
  resources :photos
end

Instead of photos_path my path is album_photos_path In my photos/new.html.erb I'm getting this error:

undefined method photos_path' for #<#<Class:0x5232b40>:0x3cd55a0>

How I can do to instead of photos_path simple form write album_photos_path ?

My new.html.erb

<%= simple_form_for([@album, @photo]) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
</div>

<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Manriquez answered 1/8, 2014 at 18:22 Comment(2)
Can you post your new action from the controller?Chancre
did you have @album and @[email protected] in your photos controller new action.Ungodly
R
12

You can specify url in your form. Like this:

<%= simple_form_for @photo, url: album_photos_path do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :title %>
    <%= f.input :description %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

But your code should also work. In your new action did you initialize both @album and @photo, something like:

def new
  @album = Album.find params[:album_id]
  @photo = @album.pictures.build
end

P.S above code is just a sample code and if both variables(@album and @photo) are initialized properly then rails will automatically generate correct url.

Roadability answered 1/8, 2014 at 18:30 Comment(3)
@max to answer the things that you mentioned, 1st part is not a lame attempt at burying error, it's providing you another way to add a url explicitly in the form. Also, i don't see how 2nd part of my answer is different from what you've posted. I can see that you are initializing album using album_id, but that just depends on the use case. Do you know if the OP is to attach photos to existing album? If you'll look at the comments below the question someone has already asked him to add controller codeRoadability
@max Also, if you'll check my answer again, i've explicitly mentioned that his code would work if album and photo objects are correctly initialized in the new action, specifying "something like" before adding the sample codeRoadability
I can't tell if you you don't understand the question or the concept of nested routes. Passing an unsaved album will also result in an error as it does not have an id.Cellular
C
1

You need to set the parent resource.

class PhotosController < ApplicationController
  before_action :set_album

  # GET /albums/1/photos/new
  def new
    @photo = @album.photos.new
  end

  # POST /albums/1/photos
  def create
    @photo = @album.photos.new(photo_params)
    # ...
  end

  private

  # ...

  def set_album
    @album = Album.find(params[:album_id])
  end
end

The reason rails is trying to call photos_path is that the polymorphic route helpers which turn an array of models into a route helper method compact the array. url_for([nil, Photo.new]) will result in the same result as url_for(Photo.new) - photos_path.

Cellular answered 29/4, 2021 at 21:2 Comment(1)
I chose to write an answer 8 years later as this question is still referenced and the accepted answer to this question does not actually fix the issue and just introduces new bugs.Cellular

© 2022 - 2025 — McMap. All rights reserved.