路线不工作 - 非常混乱

时间:2013-02-11 18:24:47

标签: ruby-on-rails ruby-on-rails-3 model-view-controller controller routes

我理解路线的概念,但现在他们只是不按预期工作。在这个特定的情况下,我试图让编辑项目的路由工作,但我收到以下错误消息:

No route matches [POST] "/projects/8/edit"

routes.rb中:

Ampta::Application.routes.draw do
  get "home/index"

  resources :sessions, only: [:new, :create, :destroy]

  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  resources :projects do
    collection do
      get "manage" 
    end 
  end

  resources :projects
  resources :tickets
  resources :projects_users
  resources :users

  root :to => 'home#index'

end

控制器:

class ProjectsController < ApplicationController
    def edit
        // no code yet
    end
end

查看:

<% @users_projects.each do |project| %>
    <%= link_to project.title, project_path(project), :class => "manage_project_link" %>
    <%= button_to "Edit", edit_project_path(project), :class => "btn edit_button" %>
<% end %>

有什么问题?运行 rake routes

时可以找到该路径
 edit_project GET    /projects/:id/edit(.:format)       projects#edit

1 个答案:

答案 0 :(得分:3)

仔细查看错误消息:

No route matches [POST] "/projects/8/edit"

它实际上是使用动词POST而不是GET。这就是为什么它找不到路线。

您正在使用生成表单的button_to帮助程序,默认方法为POST

您只需将button_to更改为link_to