没有路线匹配[POST]“/ contacts / 1”

时间:2013-04-11 19:30:20

标签: ruby-on-rails-3 routes

应用程序/视图/接触/ show.html / ERB

<%= form_for(@contact) do |f| %>
<p id="notice"><%= notice %></p>

<p>
  <b>Firstname:</b>
  <%= @contact.firstname %>
</p>

<p>
  <b>Lastname:</b>
  <%= @contact.lastname %>
</p>

<p>
  <b>Email:</b>
  <%= @contact.email %>
</p>

<p>
  <b>Mobilephone:</b>
  <%= @contact.mobilephone %>
</p>
<% end %>

<%= link_to 'Edit', edit_contact_path(@contact) %> |
<%= link_to 'List', contacts_path %>

在我的视图/ contact / index.html.erb中我有一个按钮

<%= button_to 'show', contact %>

在我的contacts_controller.rb中我只使用自动设置,如:

def show
    @contact = Contact.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @contact }
    end
  end

在我的路线文件

match '/contacts/:id/edit', :controller => 'contacts', :action => 'edit'
  match '/contacts/contact_:id/show', :controller => 'contacts', :action => 'show'
  resources :contacts
  resources :connections
  resources :addresses
  root :to => 'contacts#index'

在运行rake路线后我得到了

/contacts/:id/edit(.:format)    contacts#edit
                       /contacts/:id/show(.:format)    contacts#show
       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
    connections GET    /connections(.:format)          connections#index
                POST   /connections(.:format)          connections#create
 new_connection GET    /connections/new(.:format)      connections#new
edit_connection GET    /connections/:id/edit(.:format) connections#edit
     connection GET    /connections/:id(.:format)      connections#show
                PUT    /connections/:id(.:format)      connections#update
                DELETE /connections/:id(.:format)      connections#destroy
      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
           root        /                               contacts#index

当我点击按钮'显示'时我得到路线错误没有路线匹配[POST]“/ contacts / 1”有人可以帮我查看我有什么错误吗?非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果您使用resources contacts提供的标准路由,可能会更容易 - 那么您只需使用<%= link_to 'show', contact_path(contact) %>

此外,该按钮应执行GET请求,而不是POST,因为未定义该路由。

如果我是你,我会删除您声明的前两个自定义路由,而只需依赖resources :contacts方法。这与GET请求相结合可以解决您的问题。