没有路由匹配[POST] - 创建新数据

时间:2013-04-14 18:19:16

标签: ruby-on-rails-3 routes

目前我正在开发一个简单的应用程序。其中有一个模型“联系人”和一个模型“地址”(地址属于联系人)。这是我的addresses_controller.rb

class AddressesController < ApplicationController
 def new
  @address = Address.new
end

 def create
    @address = current_contact.addresses.build(params[:address])
    if @address.save
      flash[:success] = "Adress created!"
      redirect_to root_url
    else
      render root_url
    end
  end
end

我的routes.rb是

Demo::Application.routes.draw do


  match '/contacts/:id/edit', :controller => 'contacts', :action => 'edit'
  match '/contacts/contact_:id/show', :controller => 'contacts', :action => 'show'
  resources :contacts do

     collection { post :import }
  end  
  root :to => 'contacts#index', :as => :home
  match '/addresses' => 'addresses#index', :via => :post
  match '/addresses/new' => 'addresses#new', :via => :post
  resources :addresses
  get 'address' =>'addresses#index', :as => :addresses
end

我的地址/ _form.html.erb

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

      <ul>
      <% @address.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :Street %><br />
    <%= f.text_field :street %>
  </div>
  <div class="field">
    <%= f.label :City %><br />
    <%= f.text_field :city %>
  </div>
  <div class="field">
    <%= f.label :Region %><br />
    <%= f.text_field :region %>
  </div>
  <div class="field">
    <%= f.label :Postalcode %><br />
    <%= f.text_field :postalcode %>
  </div>
  <div class="field">
    <%= f.label :Country %><br />
    <%= f.text_field :country %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这是我的'rake routes':

                       /contacts/:id/edit(.:format)         contacts#edit
                       /contacts/contact_:id/show(.:format) contacts#show
import_contacts POST   /contacts/import(.:format)           contacts#import
       contacts GET    /contacts(.:format)                  contacts#index
                POST   /contacts(.:format)                  contacts#create
    new_contact GET    /contacts/new(.:format)              contacts#new
   edit_contact GET    /contacts/:id/edit(.:format)         contacts#edit
        contact GET    /contacts/:id(.:format)              contacts#show
                PUT    /contacts/:id(.:format)              contacts#update
                DELETE /contacts/:id(.:format)              contacts#destroy
           home        /                                    contacts#index
      addresses GET    /addresses(.:format)                 addresses#index
                POST   /addresses(.:format)                 addresses#create
    new_address GET    /addresses/new(.:format)             addresses#new
   edit_address GET    /addresses/:id/edit(.:format)        addresses#edit
        address GET    /addresses/:id(.:format)             addresses#show
                PUT    /addresses/:id(.:format)             addresses#update
                DELETE /addresses/:id(.:format)             addresses#destroy

当我点击“提交”服务器告诉我Routing error No route matches [POST] "/address"有人可以告诉我哪里出错了吗?非常感谢你

0 个答案:

没有答案