简体   繁体   中英

No route matches [POST] “/collection/new”

I built my Rails Application an tried to include multi paperclip attachments. When I click the submit button at /collection/new It gives me the Error: No route matches [POST] "/collection/new" Here is My Code

<%= 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 %>

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

Rails.application.routes.draw do
root 'page#index'
resources :collection
end

And finally my

          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

You are suppose to post data to a "create" action, not a "new" action, "new" action usually returns the form for creating records. So, change the line:

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

to:

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

should works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM