1

我正在关注可排序列表上的 Railscast ,但我遇到了嵌套资源的问题。

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

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

路线.rb:

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

步骤控制器:

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>

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

                    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
4

1 回答 1

0

明白了,我的 routes.rb 中有错误;我没有在“步骤”下嵌套集合。这是我的修复:

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

感谢 AnkitG 为我指明了正确的方向。

于 2013-02-08T22:08:14.633 回答