【问题标题】:Let devise understand custom request让设计者理解自定义请求
【发布时间】:2013-08-30 16:24:56
【问题描述】:

我有一个 Rails 应用程序,并定制了设计视图,以添加一些与 Angular 的交互性。所以,我的输入看起来像

<input id="user_name" name="name" ng-blur="isNameBusy()" type="text">

(省略所有不必要的属性)

这意味着发送表单时的请求如下所示:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "name"=>"test", "email"=>"test@mail.com", "password"=>"[FILTERED]", "commit"=>"Sign up"}

但是 devise 不理解这个请求,因为默认情况下,devise 视图中的输入具有 name 属性,例如:

<input id="user_name" name="user[name]" ng-blur="isNameBusy()" type="text">
<input id="user_email" name="user[email]"  type="email">

请求看起来像:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "user"=>{"name"=>"test", "email"=>"test@mail.com", "password"=>"[FILTERED]"}, "commit"=>"Sign up"}

你看,字段被包裹在 user 键中。

当然,我可以在我的 js 中使用默认的设计名称,比如

 registerForm.user[name].$valid

但它看起来很丑! 如何让设计人员理解我的要求?

【问题讨论】:

    标签: ruby-on-rails angularjs devise


    【解决方案1】:

    如果您不想更改输入字段的名称属性,如下所示:

    <input id="user_name" name="user[name]" ng-blur="isNameBusy()" type="text">
    

    您必须覆盖默认的设计控制器,您可以在此处找到主控制器:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb

    然后根据您的自定义参数对其进行修改。

    此答案仅在您想管理会话时专用,否则用户模型的“经典”控制器是可以的(例如创建新用户)。

    【讨论】:

    • 是的,会话/注册控制器。我知道覆盖设计控制器,但不知道如何让他在没有 user 键的情况下获取我的参数。这就是问题
    • @justso 您的表单是用于创建新用户还是用于登录?
    • 上面的表格是用来登录的,但是我的注册表也有同样的问题。
    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多