无法升级Redmine插件的Rails 2路由

时间:2012-11-28 05:01:51

标签: ruby-on-rails routes redmine

我正在尝试为Rails 2设计一个Redmine插件,以便与Rails 3一起使用。

https://github.com/dalyons/redmine-todos-scrum-plugin

我已经修复了大部分内容,但在使路线无法工作方面没有任何成功 Rails 2 的原始路线如下:

map.resources :todos, :name_prefix => 'project_', :path_prefix => '/projects/:project_id',
  :member => {:toggle_complete => :post }, :collection => {:sort => :post}

map.resources :todos, :name_prefix => 'user_', :path_prefix => '/users/:user_id', :controller => :mytodos,
  :member => {:toggle_complete => :post }, :collection => {:sort => :post}

map.my_todos 'my/todos', :controller => :mytodos, :action => :index
map.connect 'projects/:project_id/todos/show/:id', :controller => "todos", :action => "show"

rake routes输出以下内容:

          sort_project_todos POST    /projects/:project_id/todos/sort(.:format)                 {:controller=>"todos", :action=>"sort"}
               project_todos GET     /projects/:project_id/todos(.:format)                      {:controller=>"todos", :action=>"index"}
                             POST    /projects/:project_id/todos(.:format)                      {:controller=>"todos", :action=>"create"}
            new_project_todo GET     /projects/:project_id/todos/new(.:format)                  {:controller=>"todos", :action=>"new"}
toggle_complete_project_todo POST    /projects/:project_id/todos/:id/toggle_complete(.:format)  {:controller=>"todos", :action=>"toggle_complete"}
           edit_project_todo GET     /projects/:project_id/todos/:id/edit(.:format)             {:controller=>"todos", :action=>"edit"}
                project_todo GET     /projects/:project_id/todos/:id(.:format)                  {:controller=>"todos", :action=>"show"}
                             PUT     /projects/:project_id/todos/:id(.:format)                  {:controller=>"todos", :action=>"update"}
                             DELETE  /projects/:project_id/todos/:id(.:format)                  {:controller=>"todos", :action=>"destroy"}
             sort_user_todos POST    /users/:user_id/todos/sort(.:format)                       {:controller=>"mytodos", :action=>"sort"}
                  user_todos GET     /users/:user_id/todos(.:format)                            {:controller=>"mytodos", :action=>"index"}
                             POST    /users/:user_id/todos(.:format)                            {:controller=>"mytodos", :action=>"create"}
               new_user_todo GET     /users/:user_id/todos/new(.:format)                        {:controller=>"mytodos", :action=>"new"}
   toggle_complete_user_todo POST    /users/:user_id/todos/:id/toggle_complete(.:format)        {:controller=>"mytodos", :action=>"toggle_complete"}
              edit_user_todo GET     /users/:user_id/todos/:id/edit(.:format)                   {:controller=>"mytodos", :action=>"edit"}
                   user_todo GET     /users/:user_id/todos/:id(.:format)                        {:controller=>"mytodos", :action=>"show"}
                             PUT     /users/:user_id/todos/:id(.:format)                        {:controller=>"mytodos", :action=>"update"}
                             DELETE  /users/:user_id/todos/:id(.:format)                        {:controller=>"mytodos", :action=>"destroy"}
                    my_todos         /my/todos                                                  {:controller=>"mytodos", :action=>"index"}
                                     /projects/:project_id/todos/show/:id                       {:controller=>"todos", :action=>"show"}

我最接近 Rails 3 的内容如下:

scope '/projects/:project_id', :name_prefix => 'project_' do
  resources :todos, :controller => 'todos' do
    member do
      post :toggle_complete
    end

    collection do
      post :sort
    end
  end
end

scope '/users/:user_id', :name_prefix => 'user_' do
  resources :todos, :controller => 'mytodos' do
    member do
      post :toggle_complete
    end

    collection do
      post :sort
    end
  end
end

match 'my/todos' => 'mytodos#index', :as => :my_todos
match 'projects/:project_id/todos/show/:id' => 'todos#show'

rake routes输出以下内容:

toggle_complete_todo POST    /projects/:project_id/todos/:id/toggle_complete(.:format)  todos#toggle_complete {:name_prefix=>"project_"}
          sort_todos POST    /projects/:project_id/todos/sort(.:format)                 todos#sort {:name_prefix=>"project_"}
               todos GET     /projects/:project_id/todos(.:format)                      todos#index {:name_prefix=>"project_"}
                     POST    /projects/:project_id/todos(.:format)                      todos#create {:name_prefix=>"project_"}
            new_todo GET     /projects/:project_id/todos/new(.:format)                  todos#new {:name_prefix=>"project_"}
           edit_todo GET     /projects/:project_id/todos/:id/edit(.:format)             todos#edit {:name_prefix=>"project_"}
                todo GET     /projects/:project_id/todos/:id(.:format)                  todos#show {:name_prefix=>"project_"}
                     PUT     /projects/:project_id/todos/:id(.:format)                  todos#update {:name_prefix=>"project_"}
                     DELETE  /projects/:project_id/todos/:id(.:format)                  todos#destroy {:name_prefix=>"project_"}
                     POST    /users/:user_id/todos/:id/toggle_complete(.:format)        mytodos#toggle_complete {:name_prefix=>"user_"}
                     POST    /users/:user_id/todos/sort(.:format)                       mytodos#sort {:name_prefix=>"user_"}
                     GET     /users/:user_id/todos(.:format)                            mytodos#index {:name_prefix=>"user_"}
                     POST    /users/:user_id/todos(.:format)                            mytodos#create {:name_prefix=>"user_"}
                     GET     /users/:user_id/todos/new(.:format)                        mytodos#new {:name_prefix=>"user_"}
                     GET     /users/:user_id/todos/:id/edit(.:format)                   mytodos#edit {:name_prefix=>"user_"}
                     GET     /users/:user_id/todos/:id(.:format)                        mytodos#show {:name_prefix=>"user_"}
                     PUT     /users/:user_id/todos/:id(.:format)                        mytodos#update {:name_prefix=>"user_"}
                     DELETE  /users/:user_id/todos/:id(.:format)                        mytodos#destroy {:name_prefix=>"user_"}
            my_todos         /my/todos(.:format)                                        mytodos#index
                             /projects/:project_id/todos/show/:id(.:format)             todos#show

我猜我没有正确使用:name_prefix,导致重复的路径被忽略。

非常感谢任何帮助。

修改
我不确定这是否是最佳方式,但以下路线适用于 Rails 3

scope '/projects/:project_id' do
  resources :todos, :controller => 'todos', :as => 'project_todos' do
    member do
      post :toggle_complete
    end

    collection do
      post :sort
    end
  end
end

scope '/users/:user_id' do
  resources :todos, :controller => 'mytodos', :as => 'user_todos' do
    member do
      post :toggle_complete
    end

    collection do
      post :sort
    end
  end
end

match 'my/todos' => 'mytodos#index', :as => :my_todos
match 'projects/:project_id/todos/show/:id' => 'todos#show'

我从:name_prefix删除了scope,并将:as添加到resource

0 个答案:

没有答案