Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:未定义的方法'photos_path';?_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails:未定义的方法'photos_path';?

Ruby on rails Rails:未定义的方法'photos_path';?,ruby-on-rails,Ruby On Rails,嗨,我目前正在使用嵌套资源 路线 Pholder::Application.routes.draw do resources :users do resources :albums do resources :photos end end end 我有3个模型(用户、相册、照片)。我已经成功地注册了用户并创建了相册,但我一直在尝试制作照片表单。创建相册时,用户将重定向到“相册/显示”页面: 专辑/节目 <% if @album.photos.any? %> yes

嗨,我目前正在使用嵌套资源

路线

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) %>
我怀疑我把错误的信息放在了控制器上(我仍然不知道该把什么放在控制器上。)?这是我的照片控制器

照片控制器

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
class photocontroller
我的表格不对吗?对于我来说,在控制器中放置什么以及错误是发生在表单中还是控制器中都很困惑。谢谢


如果您需要更多信息,请告诉我。

同样,您必须向
帮助程序的
链接提供父资源,您也必须将其提供给表单。因此,将表单行更改为:

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

。。它应该会起作用

这里发生的事情是Rails使用了一点反射来查看传递给表单助手的对象,我猜它只是一个照片对象。默认情况下,Rails将查找照片\u路径,因为POST请求通常会在那里执行创建操作。因此,它将发布到/photos,不幸的是,根据您当前的路线,它并不存在

如果将该表单辅助线更改为:

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


这将导致它发布到/users/(userid)/相册/(相册id)/照片中,这将创建新照片。

完美!!!谢谢。因此,对于嵌套资源,当您创建表单时,您必须为父(?)资源+目标资源创建表单?很好的解释。我现在明白了!
<%= form_for [@user,@album,@photo], html: { multipart: true} do |f| %>