嗨我目前正在使用嵌套资源。Rails:未定义的方法`photos_path'?
路线
Pholder::Application.routes.draw do
resources :users do
resources :albums do
resources :photos
end
end
end
我有3个型号(用户,相册,照片)。我已经设法注册用户并创建相册,但我一直试图为照片制作表格。在创作的专辑,用户被重定向到相册/显示页面:
专辑/节目
<% if @album.photos.any? %>
yes pics
<% else %>
no pics
<% end %>
<%= link_to "Upload new pics!", new_user_album_photo_path(@user, @album) %>
,你可以看到,在页面的底部有新的照片的路径,这就是问题所在。当我点击链接,它给了我一个错误:
undefined method
photos_path”为#<#:在该页面的行#3时0x007fb69e167220>`
错误(照片/新)
照片/新
<% provide(:title, "Upload pictures") %>
<%= form_for(@photo, :html => { :multipart => true }) do |f| %>
<%= f.file_field :photo %>
<% end %>
我怀疑是我把错误的信息传递给控制器(我仍然在放什么在控制非常不稳定。)?这是我的照片控制器。
照片控制器
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
是我的形式不正确的?我很难理解要在控制器中放置什么,以及错误是发生在窗体还是控制器中。谢谢
让我知道你是否需要任何更多信息。
完美!!!非常感谢。所以对于嵌套资源,当您创建表单时,您必须为父(?)资源+您所针对的资源制作表单。 – Edmund