轨道设计简单的形式不保存所有字段 [英] Rails devise simple form not saving all fields
问题描述
当我去创建设计视图时,我想修改新的注册页面,以捕获不仅仅是电子邮件和密码。我的设计用户模型有像名字
姓氏
地址
等等。
When I went to generate the devise views I wanted to modify the new registration page to capture more than just the email & password. My devise user model has fields like first name
last name
address
and many more.
这是我现在的表单
<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>
当我提交表单时,如何才能保存电子邮件和密码?
How come when I submit the form only email and password get saved?
推荐答案
看看 Rails和Devise 示例应用程序从RailsApps项目。我还写了一个 Rails Devise教程 ,详细说明如何向Devise注册表单添加属性。
Take a look at the Rails and Devise example application from the RailsApps project. I've also written a Rails Devise Tutorial that explains in detail how to add attributes to a Devise sign-up form.
您需要告诉Devise附加属性是允许的参数。否则,Rails 4.0中引入的强参数安全性措施将简单地删除未经许可的参数,创建新用户而不设置其他属性。
You need to tell Devise that the additional attributes are "permitted parameters." Otherwise, the "strong parameters" security measure introduced in Rails 4.0 will simply drop the unpermitted parameters, creating a new user without setting the additional attributes.
有三种不同的方式在Devise中添加允许的参数:
There are three different ways to add permitted parameters in Devise:
- 覆盖默认的Devise注册控制器
- 将代码添加到应用程序控制器
- 添加一个初始化程序文件
第三种方法是最简单的。将文件添加到您的应用程序中:
The third way is the simplest. Add a file to your application:
# 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以适应其他属性。如果要添加其他属性或不同的属性,请修改这两个语句。
You can see that you pass the additional parameters to the devise_parameter_sanitizer
method. These two lines tell Devise to accommodate additional attributes. If you want to add other attributes, or different attributes, modify these two statements.
该文件的其余部分实现了Rails 关注。关注的问题是可以混合到模型和控制器中以添加共享代码的模块。通常,这些模块位于 app / controllers / concern / 文件夹中,并添加到具有 include
关键字的控制器中。在这种情况下,我们使用Ruby发送方法将我们的mixin添加到 DeviseController
对象中,将 include DevisePermittedParameters
添加到DeviseController没有实际编辑代码。
The rest of the file implements a Rails concern. Concerns are modules that can be mixed into models and controllers to add shared code. Typically, these modules go in a app/controllers/concerns/ folder and are added to a controller with an include
keyword. In this case, we use the Ruby send method to add our mixin to the DeviseController
object, adding include DevisePermittedParameters
to the DeviseController without actually editing the code.
进行这些更改并尝试应用程序后,您应该在用户记录中看到其他属性。如果不起作用,请向我们展示更多您的代码,包括用户模型和控制器。
After making these changes and trying the application, you should see the additional attributes in the user record. If it doesn't work, show us more of your code, including the user model and controller.
这篇关于轨道设计简单的形式不保存所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!