0

app/view/contact/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

当我单击“显示”按钮时,我得到了 Routes Error No route matches [POST] "/contacts/1" 有人可以帮我检查一下我犯了什么错误吗?非常感谢您的帮助。

4

1 回答 1

0

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

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

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

于 2013-04-11T19:51:59.770 回答