2016-12-30 49 views
-1

我建立了我的Rails應用程序試圖 包括多回形針附件。 當我點擊/收集/新提交按鈕 它給我的錯誤:沒有路由匹配[POST]「/收集/新」 這裏是我的代碼沒有路線匹配[郵政]「/收集/新」

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 

回答

1

您想將數據發佈到 「創造」 的行動,而不是一個 「新」 行動, 「新」 行動通常會返回創建記錄的表單。因此,更改行:

<%= form_for :@collection, url: new_collection_path, :html => { multipart: true } do |f| %> 

到:

<%= form_for @collection,:html => { multipart: true } do |f| %> 

應該作品。

+0

當我去/收集/新我給了我另一個錯誤**未定義的方法'collections_path'爲#<#:0x83e23c6c> ** – McPlum