【问题标题】:Rails devise simple form not saving all fieldsRails 设计了简单的表单,不保存所有字段
【发布时间】:2014-06-18 19:12:33
【问题描述】:

当我去生成设计视图时,我想修改新的注册页面以捕获不仅仅是电子邮件和密码。我的设计用户模型具有 first name last name address 等字段。

这是我现在的表格

<div class="row">
    <div class="col-sm-6 col-sm-offset-3">
        <h2>Sign up</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: {class:'form-horizontal'}) do |f| %>
  <%= f.error_notification %>

    <div class="row">
        <%= f.input :email, required: true, autofocus: true %>
        <%= f.input :password, required: true %>
        <%= f.input :password_confirmation, required: true %>
    </div>
    <div class="row">
        <h2>About yourself</h2>
        <%= f.input :first_name, required: true %>
        <%= f.input :last_name, required: true %>
        <%= f.input :phone, required: true %>
        <%= f.input :addressL1, label: 'Address Line 1', required: true %>
        <%= f.input :addressL2, label: 'Address Line 2' %>
        <%= f.input :city, required: true %>
        <%= f.input :postalCode, required: true %>
        <%= f.input :province, required: true, collection: provinces, prompt: '<-- select -->' %>
    </div>

    <%= f.button :submit, "Sign up" %>

<% end %>

<%= render "devise/shared/links" %>

    </div>
</div>

为什么我提交表单时只保存电子邮件和密码?

【问题讨论】:

  • 让我们看看日志看看发生了什么。您是否已将属性列入白名单?
  • 你也可以展示你的设计模型吗?

标签: ruby-on-rails ruby-on-rails-4 devise twitter-bootstrap-3


【解决方案1】:

查看 RailsApps 项目中的 Rails and Devise 示例应用程序。我还写了一个 Rails Devise Tutorial,详细解释了如何向 Devise 注册表单添加属性。

您需要告诉 Devise 附加属性是“允许的参数”。否则,Rails 4.0 中引入的“强参数”安全措施将简单地删除未经允许的参数,创建新用户而不设置其他属性。

在 Devise 中添加允许的参数有三种不同的方式:

  • 覆盖默认的设计注册控制器
  • 向应用程序控制器添加代码
  • 添加初始化文件

第三种方法是最简单的。将文件添加到您的应用程序:

# config/initializers/devise_permitted_parameters.rb

module DevisePermittedParameters
  extend ActiveSupport::Concern

  included do
    before_filter :configure_permitted_parameters
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :phone << :addressL1 << :addressL2 << :city << :postalCode << :province
    devise_parameter_sanitizer.for(:account_update) << :first_name << :last_name << :phone << :addressL1 << :addressL2 << :city << :postalCode << :province
  end

end

DeviseController.send :include, DevisePermittedParameters

您可以看到您将附加参数传递给devise_parameter_sanitizer 方法。这两行告诉 Devise 容纳额外的属性。如果要添加其他属性,或者不同的属性,修改这两条语句即可。

文件的其余部分实现了 Rails关注。关注点是可以混合到模型和控制器中以添加共享代码的模块。通常,这些模块位于 app/controllers/concerns/ 文件夹中,并使用 include 关键字添加到控制器中。在这种情况下,我们使用 Ruby send 方法将我们的 mixin 添加到 DeviseController 对象,将 include DevisePermittedParameters 添加到 DeviseController 而无需实际编辑代码。

进行这些更改并尝试应用程序后,您应该会在用户记录中看到其他属性。如果它不起作用,请向我们展示更多代码,包括用户模型和控制器。

【讨论】:

  • +1 这似乎更好。我以前从未使用过它,但肯定会尝试一下。
  • 我发现第二种方法是最简单的,但我赞成这个
【解决方案2】:

Rails 4 中,随着强参数 的引入,参数清理从模型转移到了控制器级别。在向 设计模型 添加其他字段(emailpassword 除外)时,需要注意这一点。目前,您还没有这样做,这就是为什么只有 emailpassword 字段被保存在数据库中,而不是其他字段。

您可以通过ApplicationController 中的before_action 允许设计模型的附加属性来解决此问题。

class ApplicationController < ActionController::Base
  #... 
  ## Add this before_action
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    ## To permit attributes while registration i.e. sign up (app/views/devise/registrations/new.html.erb)
    devise_parameter_sanitizer.for(:sign_up) << :attrb1 << :attrb2 

    ## To permit attributes while editing a registration (app/views/devise/registrations/edit.html.erb)
    devise_parameter_sanitizer.for(:account_update) << :attrb1 << :attrb2
  end
end

在哪里,

您需要将:attrb1:attrb2 替换为您希望允许的属性名称。例如:first_name:last_name 等等。

请参阅Devise: Strong Parameters 了解更多信息。

【讨论】:

  • Kirti,您的答案是准确的,并且在设计自述文件中进行了描述,但是该方法有一个缺点。必须评估对 Web 应用程序的每个请求以确定它是否使用设计控制器。它增加了不必要的开销。请参阅我的答案以了解另一种方法。
  • 是的。我只是通过它,错过了你的评论。我更喜欢你的方法。 :) 即使我不喜欢 Devise README 中建议的不必要的开销,但我从未想过这种方法。感谢您教育丹尼尔。
  • 是的。那是真的。顺便说一句,你的书是我读的第一本 Ruby on Rails 书。我喜欢您在书中采用的逐步和端到端的方法,并且确实学到了很多东西。很高兴被链接。 :)
猜你喜欢
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
相关资源
最近更新 更多