我试图建立我的Rails应用程序 包括多个回形针附件。 当我点击 / collection / new 上的提交按钮时 它给了我错误:没有路由匹配[POST]“/ collection / new” 这是我的代码
new.html.erb
<%= form_for @collection, url: new_collection_path, :html => { multipart: true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
</br>
<%= f.label :beschreibung %>
<%= f.text_field :description %>
</br>
<%= file_field_tag "images[]", type: :file, multiple: true %>
<%= f.submit %>
<% end %>
collection_controller.rb
class CollectionController < ApplicationController
def index
end
def new
@collection = Collection.new
end
def create
@collection = Collection.new(collection_params)
if @collection.save
if params[:images]
params[:images].each { |image|
@collection.pictures.create(image: image)
}
end
redirect_to @collection
else
render action "new"
end
end
def show
@collection = Collection.find(params[:id])
end
private
def collection_params
params.require(:collection).permit(:name, :description, :photos)
end
end
的routes.rb
Rails.application.routes.draw do
root 'page#index'
resources :collection
end
最后我的 路由
Prefix Verb URI Pattern Controller#Action
root GET / page#index
collection_index GET /collection(.:format) collection#index
POST /collection(.:format) collection#create
new_collection GET /collection/new(.:format) collection#new
edit_collection GET /collection/:id/edit(.:format) collection#edit
collection GET /collection/:id(.:format) collection#show
PATCH /collection/:id(.:format) collection#update
PUT /collection/:id(.:format) collection#update
DELETE /collection/:id(.:format) collection#destroy
答案 0 :(得分:1)
您假设将数据发布到“创建”操作,而不是“新”操作,“新”操作通常会返回创建记录的表单。所以,改变这一行:
<%= form_for :@collection, url: new_collection_path, :html => { multipart: true } do |f| %>
为:
<%= form_for @collection,:html => { multipart: true } do |f| %>
应该有效。