【问题标题】:Rails Devise - how to open access to the site rootRails Devise - 如何打开对站点根目录的访问
【发布时间】:2014-11-24 11:44:35
【问题描述】:

我有一个 Rails 应用程序作为 iOS 应用程序的后端。它是由第三方开发的,我从来没有使用过 Rails,所以在这里尝试学习。我想让用户可以通过网络访问该应用,并通过主页前端为潜在用户提供有关该应用的一些信息。

听起来很简单,但该应用使用 Devise 进行用户身份验证,当我点击站点根目录时,我收到以下错误消息:

You need to sign in or sign up before continuing.

在 routes.rb 文件中我有以下内容:

root :to => "visitors#index"
devise_for :users
resources :users

在我的访问者控制器中,我添加了一个 before_filter 来尝试打开站点索引:

class VisitorsController < ApplicationController
    before_filter :authenticate_user!, :except => [:index]
end

但这并没有什么不同。这里有什么明显的我遗漏的东西,或者我没有掌握的一些概念吗?基本上我希望用户点击站点主页,然后单击“登录”链接,之后他们将可以访问站点的其余部分。

编辑:

我的应用程序控制器:

class ApplicationController < ActionController::Base
  respond_to :html, :json

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session, :if => Proc.new { |c| c.request.format == 'application/json'}
  before_action :configure_permitted_parameters, if: :devise_controller?

  acts_as_token_authentication_handler_for User

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :name
    devise_parameter_sanitizer.for(:account_update) << :name
  end

end

【问题讨论】:

  • 你能发布你的ApplicationController吗?
  • 应用程序是在production 还是development 中运行?如果production,代码可能会被缓存,并且在重新启动应用服务器之前可能看不到更改。
  • @D-side:它在我的开发机器上运行,但每次更改后我都会重新启动服务器以确保。

标签: ruby-on-rails devise


【解决方案1】:

来自Devise's how to guide,这似乎描述了您的示例。您可以在 routes.rb 文件中创建两个根。一种用于经过身份验证的用户,一种用于未经过身份验证的用户:

authenticated :user do
  root :to => 'visitors#some_action', :as => :authenticated_root
end

root :to => 'visitors#index'

但是,请发布您的ApplicationController,因为您的示例代码应该可以工作,问题可能就在那里。

【讨论】:

  • 我试过这个,但没有改变。我在上面添加了我的 ApplicationController。
  • @aritchie 你可以尝试删除你的except 并添加skip_before_filter :authenticate_user!, :only =&gt; :index
  • 没有变化,仍然出现错误。在我使用“rails s”运行站点的终端中,我收到消息:“Started GET”/” for 127.0.0.1 at 2014-11-24 12:36:02 +0000 由访客控制器#index 处理为 HTML 0ms 内完成 401 Unauthorized"
【解决方案2】:

如果其他人有同样的问题 - 罪魁祸首是 application_controller.rb 中的以下行:

acts_as_token_authentication_handler_for User

它是 simple_token_authentication Gem 的一部分 (https://github.com/gonzalo-bulnes/simple_token_authentication)

为了修复启用对根页面的匿名访问,我将其更改为以下内容:

acts_as_token_authentication_handler_for User, :except => [:index]

【讨论】:

    猜你喜欢
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多