嵌套资源索引页上的未定义方法错误

作为此前一个问题的后续问题,已经解决了

我已经开始使用rails应用程序,我可以在其中创建集合 。 每个collections集都可以有多张照片 。 我现在可以创建这些集合和照片。 但每当我尝试访问/ collections / 1 / photos时,我的照片索引中的这一行都有问题

undefined method `photo_path' for #<#:0x007fb8c88673a0>  

photos_controller.rb

 class PhotosController < ApplicationController before_action :set_photo, only: [:show, :edit, :update, :destroy] # GET /photos # GET /photos.json def index @photos = Photo.all end # GET /photos/1 # GET /photos/1.json def show end # GET /photos/new def new @photo = Photo.new end # GET /photos/1/edit def edit end # POST /photos # POST /photos.json def create @photo = Photo.new(photo_params) respond_to do |format| if @photo.save redirect_to @photo format.html { redirect_to @photo, notice: 'Photo was successfully created.' } format.json { render :show, status: :created, location: @photo } else format.html { render :new } format.json { render json: @photo.errors, status: :unprocessable_entity } end end end # PATCH/PUT /photos/1 # PATCH/PUT /photos/1.json def update respond_to do |format| if @photo.update(photo_params) format.html { redirect_to @photo, notice: 'Photo was successfully updated.' } format.json { render :show, status: :ok, location: @photo } else format.html { render :edit } format.json { render json: @photo.errors, status: :unprocessable_entity } end end end # DELETE /photos/1 # DELETE /photos/1.json def destroy @photo.destroy respond_to do |format| format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_photo @photo = Photo.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def photo_params params.require(:photo).permit(:name, :collection_id) end end 

的routes.rb

 Rails.application.routes.draw do resources :collections do resources :photos end end 

/photos/index.html

 

Photos


collection.rb

 class Collection < ApplicationRecord has_many :photos end 

photo.rb

 class Photo < ApplicationRecord belongs_to :collection end 

我使用Rails 5.0.0.1和ruby 2.3.0

使用此显示和删除链接路径是错误的

 <%= link_to 'Show', collection_photo_path(@collection, photo) %> <%= link_to 'Edit', edit_collection_photo_path(@collection, photo) %> <%= link_to 'Destroy', collection_photo_path(@collection, photo), method: :delete, data: { confirm: 'Are you sure?' } %> 

链接应该是这样的,从控制器你应该初始化@collection

 def index @collction = Collection.find(params[:collection_id]) @photos = @collction.photos end