Ruby on rails 登录错误时设计重定向:ActiveRecord::RecordNotFound in DocumentsController#show

Ruby on rails 登录错误时设计重定向:ActiveRecord::RecordNotFound in DocumentsController#show,ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,Devise,我正在尝试在登录和注册后重定向到特定页面。我有两个模型用户和资源。登录后,我想显示属于用户的文档列表。 如果没有文档,则应重定向到users/:id/document#create否则为users/:id/document#index 有什么建议我该怎么做 我在控制器中定义的重定向路径出现以下错误 ActiveRecord::DocumentsController#show中未找到RecordNot 找不到没有ID的文档 应用程序\u控制器.rb def after_sign_in_path_f

我正在尝试在登录和注册后重定向到特定页面。我有两个模型用户和资源。登录后,我想显示属于用户的文档列表。 如果没有文档,则应重定向到users/:id/document#create否则为users/:id/document#index

有什么建议我该怎么做

我在控制器中定义的重定向路径出现以下错误

ActiveRecord::DocumentsController#show中未找到RecordNot

找不到没有ID的文档

应用程序\u控制器.rb

def after_sign_in_path_for(user)
 user_documents_url(user)
end
 root :to => 'home#index'

   devise_for :users

   resources :users do
        resources :documents
   end
  devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
Routes.rb

def after_sign_in_path_for(user)
 user_documents_url(user)
end
 root :to => 'home#index'

   devise_for :users

   resources :users do
        resources :documents
   end
  devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
路线

root        /                                        home#index
        new_user_session GET    /login(.:format)                         devise/sessions#new
            user_session POST   /login(.:format)                         devise/sessions#create
    destroy_user_session DELETE /logout(.:format)                        devise/sessions#destroy
           user_password POST   /password(.:format)                      devise/passwords#create
       new_user_password GET    /password/new(.:format)                  devise/passwords#new
      edit_user_password GET    /password/edit(.:format)                 devise/passwords#edit
                         PUT    /password(.:format)                      devise/passwords#update
cancel_user_registration GET    /cancel(.:format)                        devise/registrations#cancel
       user_registration POST   /                                        devise/registrations#create
   new_user_registration GET    /sign_up(.:format)                       devise/registrations#new
  edit_user_registration GET    /edit(.:format)                          devise/registrations#edit
                         PUT    /                                        devise/registrations#update
                         DELETE /                                        devise/registrations#destroy
       user_confirmation POST   /confirmation(.:format)                  devise/confirmations#create
   new_user_confirmation GET    /confirmation/new(.:format)              devise/confirmations#new
                         GET    /confirmation(.:format)                  devise/confirmations#show
          user_documents POST   /users/:user_id/documents(.:format)      documents#create
      new_user_documents GET    /users/:user_id/documents/new(.:format)  documents#new
     edit_user_documents GET    /users/:user_id/documents/edit(.:format) documents#edit
                         GET    /users/:user_id/documents(.:format)      documents#show
                         PUT    /users/:user_id/documents(.:format)      documents#update

        /user/:user_id/documents(.:format)       user/documents#index

谢谢

我看到代码本身没有问题。但是您需要在测试之前构建一些“文档”数据,可以是FactoryGirl,也可以是存根


因为您确实没有任何用户的“文档”数据,Rails当然给您提供了RecordNotFound错误

  def after_sign_in_path_for(resource_or_scope)
     // redirect path
  end

我希望这会有所帮助

我通过遵循这一点解决了我的问题。下面是我的路线。rb

   resources :users do
        resources :documents, only: [:index]
   end
   resource :documents, except: [:index]

这将为我提供文档索引路径以及文档新路径。

您的路由不会显示文档索引操作。您确定发布了正确的routes.rb和rake routes输出吗<代码>资源:文档应生成索引操作的路径。已更新我的路径以包含文档#索引确实包含与用户关联的文档。错误显示找不到文档,因为它正在尝试显示文档,而我没有传递“文档id”。您的
索引方法是什么?它是否返回任何数据?您建议的解决方案不正确,因为没有名为Dashboard的资源。这段代码与我的项目相关,您可以根据需要进行更改。:)