嵌套资源+自定义控制器方法

时间:2013-02-08 21:57:36

标签: ruby-on-rails

我在sortable lists上关注Railscast,我遇到嵌套资源问题。

在我的应用中,“项目”有很多“步骤”,我正在为“步骤”创建一个可排序的列表。

我在步骤控制器中添加了“sort”方法并得到了错误 我尝试加载页面时,未定义的局部变量或方法`sort_project_steps_url'。如果我从tbody中删除data-update-url,页面会正确加载。链接排序的正确方法是什么?

routes.rb中:

  resources :projects do
    resources :steps
      match "steps/:id" => "steps#number", :as => :number
      collection {post :sort}
  end

steps.controller:

class StepsController < ApplicationController
  before_filter :get_project

  # Sorts steps
  def sort
    render nothing: true
  end

  private
  # get_project converts the project_id given by the routing
  # into an @project object
  def get_project
    @project = Project.find(params[:project_id])
  end
end

步骤/ show.html.erb:

<table id="stepTable">
    <tbody data-update-url="<%= sort_project_steps_url(@project) %>">
</tbody>
</table>

我的佣金路线在下面(不确定为什么排序出现在项目而不是步骤下):

                    admin_root            /admin(.:format)                               admin/dashboard#index
batch_action_admin_admin_users POST       /admin/admin_users/batch_action(.:format)      admin/admin_users#batch_action
             admin_admin_users GET        /admin/admin_users(.:format)                   admin/admin_users#index
                               POST       /admin/admin_users(.:format)                   admin/admin_users#create
          new_admin_admin_user GET        /admin/admin_users/new(.:format)               admin/admin_users#new
         edit_admin_admin_user GET        /admin/admin_users/:id/edit(.:format)          admin/admin_users#edit
              admin_admin_user GET        /admin/admin_users/:id(.:format)               admin/admin_users#show
                               PUT        /admin/admin_users/:id(.:format)               admin/admin_users#update
                               DELETE     /admin/admin_users/:id(.:format)               admin/admin_users#destroy
               admin_dashboard            /admin/dashboard(.:format)                     admin/dashboard#index
     batch_action_admin_images POST       /admin/images/batch_action(.:format)           admin/images#batch_action
                  admin_images GET        /admin/images(.:format)                        admin/images#index
                               POST       /admin/images(.:format)                        admin/images#create
               new_admin_image GET        /admin/images/new(.:format)                    admin/images#new
              edit_admin_image GET        /admin/images/:id/edit(.:format)               admin/images#edit
                   admin_image GET        /admin/images/:id(.:format)                    admin/images#show
                               PUT        /admin/images/:id(.:format)                    admin/images#update
                               DELETE     /admin/images/:id(.:format)                    admin/images#destroy
   batch_action_admin_projects POST       /admin/projects/batch_action(.:format)         admin/projects#batch_action
                admin_projects GET        /admin/projects(.:format)                      admin/projects#index
                               POST       /admin/projects(.:format)                      admin/projects#create
             new_admin_project GET        /admin/projects/new(.:format)                  admin/projects#new
            edit_admin_project GET        /admin/projects/:id/edit(.:format)             admin/projects#edit
                 admin_project GET        /admin/projects/:id(.:format)                  admin/projects#show
                               PUT        /admin/projects/:id(.:format)                  admin/projects#update
                               DELETE     /admin/projects/:id(.:format)                  admin/projects#destroy
      batch_action_admin_steps POST       /admin/steps/batch_action(.:format)            admin/steps#batch_action
                   admin_steps GET        /admin/steps(.:format)                         admin/steps#index
                               POST       /admin/steps(.:format)                         admin/steps#create
                new_admin_step GET        /admin/steps/new(.:format)                     admin/steps#new
               edit_admin_step GET        /admin/steps/:id/edit(.:format)                admin/steps#edit
                    admin_step GET        /admin/steps/:id(.:format)                     admin/steps#show
                               PUT        /admin/steps/:id(.:format)                     admin/steps#update
                               DELETE     /admin/steps/:id(.:format)                     admin/steps#destroy
   batch_action_admin_comments POST       /admin/comments/batch_action(.:format)         admin/comments#batch_action
                admin_comments GET        /admin/comments(.:format)                      admin/comments#index
                               POST       /admin/comments(.:format)                      admin/comments#create
                 admin_comment GET        /admin/comments/:id(.:format)                  admin/comments#show
        new_admin_user_session GET        /admin/login(.:format)                         active_admin/devise/sessions#new
            admin_user_session POST       /admin/login(.:format)                         active_admin/devise/sessions#create
    destroy_admin_user_session DELETE|GET /admin/logout(.:format)                        active_admin/devise/sessions#destroy
           admin_user_password POST       /admin/password(.:format)                      active_admin/devise/passwords#create
       new_admin_user_password GET        /admin/password/new(.:format)                  active_admin/devise/passwords#new
      edit_admin_user_password GET        /admin/password/edit(.:format)                 active_admin/devise/passwords#edit
                               PUT        /admin/password(.:format)                      active_admin/devise/passwords#update
                 project_steps GET        /projects/:project_id/steps(.:format)          steps#index
                               POST       /projects/:project_id/steps(.:format)          steps#create
              new_project_step GET        /projects/:project_id/steps/new(.:format)      steps#new
             edit_project_step GET        /projects/:project_id/steps/:id/edit(.:format) steps#edit
                  project_step GET        /projects/:project_id/steps/:id(.:format)      steps#show
                               PUT        /projects/:project_id/steps/:id(.:format)      steps#update
                               DELETE     /projects/:project_id/steps/:id(.:format)      steps#destroy
                project_number            /projects/:project_id/steps/:id(.:format)      steps#number
                 sort_projects POST       /projects/sort(.:format)                       projects#sort
                      projects GET        /projects(.:format)                            projects#index
                               POST       /projects(.:format)                            projects#create
                   new_project GET        /projects/new(.:format)                        projects#new
                  edit_project GET        /projects/:id/edit(.:format)                   projects#edit
                       project GET        /projects/:id(.:format)                        projects#show
                               PUT        /projects/:id(.:format)                        projects#update
                               DELETE     /projects/:id(.:format)                        projects#destroy
                        images GET        /images(.:format)                              images#index
                               POST       /images(.:format)                              images#create
                     new_image GET        /images/new(.:format)                          images#new
                    edit_image GET        /images/:id/edit(.:format)                     images#edit
                         image GET        /images/:id(.:format)                          images#show
                               PUT        /images/:id(.:format)                          images#update
                               DELETE     /images/:id(.:format)                          images#destroy
                          root            /                                              projects#index

1 个答案:

答案 0 :(得分:0)

得到它,我的routes.rb出错了;我没有在“步骤”下嵌套馆藏。这是我的修复:

  resources :projects do
    resources :steps do
        collection {post :sort}
      end
      match "steps/:id" => "steps#number", :as => :number
  end

感谢AnkitG,指出我正确的方向。