【问题标题】:Devise RegistrationsController create action in API在 API 中设计 RegistrationsController 创建操作
【发布时间】:2012-06-10 16:41:18
【问题描述】:

我正在尝试使用 API 创建 Rails 3 应用程序。我使用设计作为我的身份验证系统。我想让注册和稍后通过 API 登录成为可能。我不太确定我应该怎么做。我创建了单独的控制器来管理 API,即api/v1/account/1/projects.json。我目前正在尝试为 API 创建一个自定义 Devise::RegistrationsController,但我不太确定如何执行此操作。

我的创建操作目前如下所示:

class API::V1::RegistrationsController < Devise::RegistrationsController
  redpond_to :json, :xml

  def create
    build_resource
    if resource.save
      if resource.active_for_authentication?
        # Account is a nested resource for User, I want to 
        # create an associated account when the User signs up through the client, 
        # how should I do this?
        account = resource.accounts.build 
        # How can I get the sign_in to work?
        sign_in(resource_name, resource)
        # Probably wrong
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        # probably wrong
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      # probably wrong, or?
      clean_up_passwords resource
    end
  end

正如我的 cmets 中提到的那样,这可能是非常不正确的,所以我想知道是否有人可以给我任何关于如何使其工作的指导。还应该提到帐户是用户的嵌套资源。

我还想知道如何使用 Devise 在 API 中设置登录和注销功能。

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby api ruby-on-rails-3.1 devise


    【解决方案1】:

    This 其他 SO 问题可能会对您有所帮助。它看起来非常相似。请特别注意答案中链接的gist

    【讨论】:

    • 谢谢,我去看看。
    • 我按照链接进行了签名部分的工作,并进行了一些修改。因此,注册部分也可能会起作用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2015-02-07
    • 2012-03-16
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多