【问题标题】:Routing Error No route matches {:action=>"show", :controller=>"dashboard"}路由错误没有路由匹配 {:action=>"show", :controller=>"dashboard"}
【发布时间】:2013-03-21 06:32:16
【问题描述】:

我希望当管理员登录时,他应该被重定向到“仪表板”页面,如果普通用户登录,他们应该被重定向到应用程序的根目录,即“posts#index”。 当普通用户注册时,它完美地工作意味着它将用户带到应用程序的根路径。 但是当管理员登录时,它显示“路由错误没有路由匹配 {:action=>"show", :controller=>"dashboard"}" 错误。我在用户表中为管理员添加了 is_Admin 列。

ApplicationController.rb

class ApplicationController < ActionController::Base
  protect_from_forgery
  def after_sign_in_path_for(user)
    if current_user.is_Admin?
      redirect_to  dashboard_path
    else
      :root
    end
  end
end

Dashboard_controller.rb

class DashboardController < ApplicationController
  def index
    @users = User.all
  end
end

views/dashboard/index.html.erb

<h2>Admin Area</h2>
<table>
  <tr>
    <th> User Email </th>
    <th> User Registered Time</th>
    <th> Login Count</th>
  </tr>
  <% @users.each do |user| %>
    <tr>
      <td><%= user.email %></td>
      <td><%= user.created_at %></td>
      <td><%= user. sign_in_count %></td>
    </tr>
  <% end %>
</table>

routes.rb

Infra::Application.routes.draw do
  devise_for :users

  resources :posts
  root :to => "posts#index"
  #match "dashboard" => "dashboard#index"

  resources :dashboard 
end

rake 路由输出如下:

 rake routes
       new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
           user_session POST   /users/sign_in(.:format)       devise/sessions#create
   destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
          user_password POST   /users/password(.:format)      devise/passwords#create
      new_user_password GET    /users/password/new(.:format)  devise/passwords#new
     edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                        PUT    /users/password(.:format)      devise/passwords#update
ancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
      user_registration POST   /users(.:format)               devise/registrations#create
  new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
 edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                        PUT    /users(.:format)               devise/registrations#update
                        DELETE /users(.:format)               devise/registrations#destroy
                  posts GET    /posts(.:format)               posts#index
                        POST   /posts(.:format)               posts#create
               new_post GET    /posts/new(.:format)           posts#new
              edit_post GET    /posts/:id/edit(.:format)      posts#edit
                   post GET    /posts/:id(.:format)           posts#show
                        PUT    /posts/:id(.:format)           posts#update
                        DELETE /posts/:id(.:format)           posts#destroy
                   root        /                              posts#index
        dashboard_index GET    /dashboard(.:format)           dashboard#index
                        POST   /dashboard(.:format)           dashboard#create
          new_dashboard GET    /dashboard/new(.:format)       dashboard#new
         edit_dashboard GET    /dashboard/:id/edit(.:format)  dashboard#edit
              dashboard GET    /dashboard/:id(.:format)       dashboard#show
                        PUT    /dashboard/:id(.:format)       dashboard#update
                        DELETE /dashboard/:id(.:format)       dashboard#destroy

堆栈树

Started POST "/users/sign_in" for 127.0.0.1 at 2013-03-31 01:28:36 +0530
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"j5KOmGDpI7Xwit4U7JUysB3EqhIogljjCDlSIK158/s=", "user"=>{"email"=>"vieenaysiingh@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
  [1m[36mUser Load (1.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."email" = 'vieenaysiingh@gmail.com' LIMIT 1[0m
  [1m[35m (0.0ms)[0m  begin transaction
  [1m[36m (2.0ms)[0m  [1mUPDATE "users" SET "last_sign_in_at" = '2013-03-30 19:44:14.252484', "current_sign_in_at" = '2013-03-30 19:58:37.089835', "sign_in_count" = 50, "updated_at" = '2013-03-30 19:58:37.091835' WHERE "users"."id" = 1[0m
  [1m[35m (195.0ms)[0m  commit transaction
Redirected to http://localhost:3000/dashboard
Completed 500 Internal Server Error in 423ms

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".):
  actionpack (3.2.12) lib/action_controller/metal/rendering.rb:15:in `render'
  actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
  activesupport (3.2.12) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
  activesupport (3.2.12) lib/active_support/core_ext/benchmark.rb:5:in `ms'
  actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
  actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
  activerecord (3.2.12) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
  actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:39:in `render'
  actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
  actionpack (3.2.12) lib/action_controller/metal/responder.rb:232:in `default_render'
  actionpack (3.2.12) lib/action_controller/metal/responder.rb:160:in `to_html'
  actionpack (3.2.12) lib/action_controller/metal/responder.rb:153:in `respond'
  actionpack (3.2.12) lib/action_controller/metal/responder.rb:146:in `call'
  actionpack (3.2.12) lib/action_controller/metal/mime_responds.rb:239:in `respond_with'
  devise (2.2.3) app/controllers/devise/sessions_controller.rb:18:in `create'
  actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
  actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (3.2.12) lib/active_support/callbacks.rb:458:in `_run__176658777__process_action__917642946__callbacks'
  activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.12) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.12) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.12) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.12) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.12) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  actionpack (3.2.12) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  activerecord (3.2.12) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (3.2.12) lib/abstract_controller/base.rb:121:in `process'
  actionpack (3.2.12) lib/abstract_controller/rendering.rb:45:in `process'
  actionpack (3.2.12) lib/action_controller/metal.rb:203:in `dispatch'
  actionpack (3.2.12) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  actionpack (3.2.12) lib/action_controller/metal.rb:246:in `block in action'
  actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `call'
  actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:36:in `call'
  actionpack (3.2.12) lib/action_dispatch/routing/mapper.rb:42:in `call'
  journey (1.0.4) lib/journey/router.rb:68:in `block in call'
  journey (1.0.4) lib/journey/router.rb:56:in `each'
  journey (1.0.4) lib/journey/router.rb:56:in `call'
  actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:601:in `call'
  warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.1) lib/warden/manager.rb:34:in `catch'
  warden (1.2.1) lib/warden/manager.rb:34:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.5) lib/rack/etag.rb:23:in `call'
  rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/head.rb:14:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
  rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/cookies.rb:341:in `call'
  activerecord (3.2.12) lib/active_record/query_cache.rb:64:in `call'
  activerecord (3.2.12) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `_run__395807340__call__550232966__callbacks'
  activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.12) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.12) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.12) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.12) lib/rails/engine.rb:479:in `call'
  railties (3.2.12) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.12) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.0ms)
  Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (4.0ms)
  Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (103.0ms)

我在 application_controller.rb 中定义了“def after_sign_in_path_for(user)”,它根据用户类型决定重定向到哪里。我想,问题就在这里。

【问题讨论】:

  • 使用resource :dashboard

标签: ruby-on-rails ruby controller routes


【解决方案1】:

改变

resources :dashboard 

resource :dashboard 

【讨论】:

  • 我将资源 :dashboard 更改为资源 :dashboard 但现在我收到“AbstractController::DoubleRenderError in Devise::SessionsController#create”错误。详细信息如下“在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,每个操作最多调用一次。另请注意,重定向和渲染都不会终止操作的执行,所以如果你想在重定向后退出一个动作,你需要做一些类似“redirect_to(...) and return”的事情。
【解决方案2】:

1) - 每当您更改路线时,您可能应该重新启动 rails 进程

然后我会尝试在仪表板中使用匹配代替资源(猜你不需要所有的 CRUD 操作?)

match "dashboard" => "dashboard#index"

重定向应该是

redirect_to dashboard_path

如果您确实使用资源,则为 redirect_to dashboards_path(索引操作的复数)

路线的顺序也很重要,也许让你的比赛更靠近路线的顶部?

另请参阅:http://guides.rubyonrails.org/routing.html

【讨论】:

  • @house9,谢谢,但在实施 redirect_to dashboards_path(索引操作的复数)后,我收到“路由错误没有路由匹配 {:action=>"show", :controller=>"dashboard" , :id=>nil}" 错误。
  • 尝试dashboard_index_path(基于您的rake routes 输出)
  • 我尝试了上述建议,但我收到“AbstractController::DoubleRenderError in Devise::SessionsController#create”错误消息。详细信息如下:“在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,每个操作最多调用一次。另请注意,重定向和渲染都不会终止操作的执行,因此如果你想在重定向后退出一个动作,你需要做一些类似“redirect_to(...) and return”的操作。
  • 好的 - 这意味着路由正在运行:) - 向我们展示堆栈跟踪,以便我们帮助调试
  • AbstractController::DoubleRenderError (在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,并且每个操作最多调用一次。另请注意,无论是重定向还是render 终止动作的执行,所以如果你想在重定向后退出一个动作,你需要做类似“redirect_to(...) and return”之类的事情。): actionpack (3.2.12) lib/action_controller/metal/rendering .rb:15:in render' actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:40:in block(2 级)在渲染中的 activesupport (3.2.12)
【解决方案3】:

方法 after_sign_in_path_for 应该返回路径,而不是重定向。

class ApplicationController < ActionController::Base
  protect_from_forgery
  def after_sign_in_path_for(user)
    if current_user.is_Admin?
      dashboard_index_path
    else
      :root
    end
  end
end

【讨论】:

  • class ApplicationController
  • 大家好,类 ApplicationController
  • 使用“/dashboard”并不美观。 dashboard_path 没有帮助?
  • dashboard_path 显示错误,这就是我使用“/dashboard”的原因。虽然我会尝试为您的建议找出解决方案。
  • 然后尝试,正如 house9 所写,dashboard_index_path,应该可以工作 =)
【解决方案4】:

使用它来让它工作:

    redirect_to main_app.root_path

将 main_app 添加到您的路由路径

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多