您好,我目前正在处理嵌套资源。
路线
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) %>
如您所见,在页面底部有一个新照片的路径,这就是问题所在。当我点击链接时,出现错误:
未定义的方法
#<#:0x007fb69e167220>` 的 photos_path'
错误发生在该页面的第 3 行(照片/新)
照片/新
<% provide(:title, "Upload pictures") %>
<%= form_for(@photo, :html => { :multipart => true }) do |f| %>
<%= f.file_field :photo %>
<% end %>
我怀疑我向 Controller 输入了错误的信息(我仍然不确定要向 Controller 输入什么信息。)?这是我的照片 Controller 。
照片 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
我的表格不正确吗?令我困惑的是在 Controller 中放入什么以及错误是在表单中还是在 Controller 中发生。谢谢
如果您需要更多信息,请告诉我。
最佳答案
与您必须向 link_to
帮助程序提供父资源的方式相同,您还必须将其提供给表单。因此,将表单行更改为:
<%= form_for([@user, @album, @photo], :html => { :multipart => true }) do |f| %>
.. 它应该可以工作!
关于ruby-on-rails - rails : undefined method `photos_path' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12711907/