1

並べ替え可能なリストでRailscastをフォローしていますが、ネストされたリソースに問題があります。

私のアプリでは、「プロジェクト」には多くの「ステップ」があり、「ステップ」の並べ替え可能なリストを作成しています。

メソッド "sort" をステップ コントローラーに追加しましたが 、ページを読み込もうとすると、未定義のローカル変数またはメソッド `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>

私のレーキ ルートは以下のとおりです (並べ替えがプロジェクトの下に表示され、ステップでは表示されない理由がわかりません):

                    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 に答える