Ruby on rails 没有与[POST]匹配的路线”/收藏/1/照片/new";

Ruby on rails 没有与[POST]匹配的路线”/收藏/1/照片/new";,ruby-on-rails,ruby,Ruby On Rails,Ruby,有人能帮我解决这个错误吗 我刚刚开始了一个基本的Rails项目,在那里我有系列。每个收藏都可以有多张照片 但是,我可以创建这些集合。但是,无论何时我想要创建附加到收藏的照片,都会出现以下错误: No route matches [POST] "/collections/1/photos/new" routes.rb Rails.application.routes.draw do resources :collections do resources :photos e

有人能帮我解决这个错误吗

我刚刚开始了一个基本的Rails项目,在那里我有系列。每个收藏都可以有多张照片

但是,我可以创建这些集合。但是,无论何时我想要创建附加到收藏的照片,都会出现以下错误:

No route matches [POST] "/collections/1/photos/new"
routes.rb

Rails.application.routes.draw do    
  resources :collections do
    resources :photos
  end
end
收集控制器.rb

class CollectionsController < ApplicationController
  before_action :set_collection, only: [:show, :edit, :update, :destroy]

  # GET /collections
  # GET /collections.json
  def index
    @collections = Collection.all
  end

  # GET /collections/1
  # GET /collections/1.json
  def show
  end

  # GET /collections/new
  def new
    @collection = Collection.new
  end

  # GET /collections/1/edit
  def edit
  end

  # POST /collections
  # POST /collections.json
  def create
    @collection = Collection.new(collection_params)

    respond_to do |format|
      if @collection.save
        format.html { redirect_to @collection, notice: 'Collection was successfully created.' }
        format.json { render :show, status: :created, location: @collection }
      else
        format.html { render :new }
        format.json { render json: @collection.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /collections/1
  # PATCH/PUT /collections/1.json
  def update
    respond_to do |format|
      if @collection.update(collection_params)
        format.html { redirect_to @collection, notice: 'Collection was successfully updated.' }
        format.json { render :show, status: :ok, location: @collection }
      else
        format.html { render :edit }
        format.json { render json: @collection.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /collections/1
  # DELETE /collections/1.json
  def destroy
    @collection.destroy
    respond_to do |format|
      format.html { redirect_to collections_url, notice: 'Collection was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_collection
      @collection = Collection.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def collection_params
      params.require(:collection).permit(:name, :description)
    end
end
class CollectionsController
照片\u 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
class photocontroller
照片/new.html.erb

<h1>New Photo</h1>

<%= render 'form', photo: @photo %>

<%= link_to 'Back', collection_photos_path %>
新照片
照片/_form.html.erb

<%= form_for [@collection, @photo], url: new_collection_photo_path do |f| %>
  <% if photo.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(photo.errors.count, "error") %> prohibited this photo from being saved:</h2>

      <ul>
        <% photo.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>Naam:</p>
  <%= f.text_field :name %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

禁止保存此照片:
纳姆:


您的表单的URL错误

new\u collection\u photo\u path

应该是


collection\u photos\u path

它修复了错误。但是由于某种原因,我的photos_控制器中出现了以下错误:未定义的方法'photo_url'for#你的意思是什么?photo_params Extracted source(第31行附近):respond_to do | format | if@photo.save redirect_to@photo format.html{redirect_to@photo,注意:'照片已成功创建。}format.json{render:show,status::created,location:@photo} else@royketelaar抱歉,无法理解这种格式,你能提出新问题吗?因为这是新问题。是的,很难提出评论,我会提出新问题