我在我的应用中使用 Devise + CanCan + Rolify 作为授权和身份验证解决方案。
管理员在我的应用程序中通过他们的仪表板进行所有用户访问管理。所以我试图从我的“admin_dashboard.index.html 文件”中访问所有 Devise View 链接
我在此应用中创建了一个用户 Controller
这里是 admin_dashboard_index.html.erb 文件
<table id="users_index_table" %>
<tr>
<th>id</th>
<th>user name</th>
<th>user email</th>
<th>user role</th>
<th>reset password</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.roles %></td>
<th><%= link_to 'reset password', edit_user_password_path %></th>
<td><%= link_to %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<%= link_to 'New User', new_user_registration_path %>
现在的问题是,当我单击这些“用户”链接时,除了“new_user_registration_path”外,其他所有链接都有效。当我单击它时,它会将我带到应用程序的主页,并且 Rails 服务器显示此跟踪:
Started GET "/users/sign_up" for 127.0.0.1 at 2012-06-16 18:24:49 -0700
Processing by Devise::RegistrationsController#new as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.5ms)
我怎样才能让新用户链接和“edit_user_password_path”工作并将我引导到适当的字段而不是主页。
谢谢
最佳答案
根据服务器输出,似乎有一个过滤器不允许您在已登录时创建新用户 (:require_no_authentication)。在研究了设计的设置方式后,他们的注册 Controller 上确实有一个前置过滤器,需要未经身份验证的 session 才能创建新的用户注册。
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
如果您的用户 Controller 是从 Devise::RegistrationsController 继承的,那么您可以完全跳过过滤器:
skip_before_filter :require_no_authentication, :only => [:new, :create]
或者您可以编写自己的 :require_no_authentication 方法来检查登录用户是管理员还是普通用户。您可以将此方法放在用户 Controller 的底部:
protected
def require_no_authentication
if current_user.is_admin?
return true
else
return super
end
end
如果您的 Controller 不是从 Devise::RegistrationsController 继承的,那么您需要在设置路由时指定您的自定义 Controller 。
devise_for :users, :controllers => { :registration => "my/controller" }
然后显然实现您自己的"new"方法。
我发现了一些与此问题相关的现有堆栈溢出问题:
rails:3 Devise signup Filter chain halted as :require_no_authentication rendered or redirected
关于ruby-on-rails - rails 应用程序 : Links to Devise views route to the home page.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11068381/