我有两张桌子员工和公司。我想在员工注册时使用设计注册操作注册员工的公司名称。如何编写设计参数清理方法以在员工注册时保存公司名称?
问问题
1304 次
2 回答
0
在注册表单中添加自定义信息试试这个:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit( :company_name, :email,:password, :password_confirmation ) }
end
end
并更改您的意见/设计/注册/new.html.erb。
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :company_name %><br />
<%= f.text_field :company_name %>
</div>
<% end %>
于 2015-07-26T08:57:03.873 回答
0
诀窍是使用accepts_nested_attributes_for并覆盖sign_up_params
注册控制器上的方法。
1.设置用户模型以接受公司的属性
class User < ActiveRecord::Base
belongs_to :company
accepts_nested_attributes_for :company
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
2.覆盖默认注册控制器
# config/routes.rb
Rails.application.routes.draw do
# ...
devise_for :users, controllers: { registrations: "registrations" }
end
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
# GET /users/sign_up
def new
@user = User.new(company: Company.new)
end
def sign_up_params
params.require(:user).permit(
:email, :password, :password_confirmation,
company_attributes: [:name]
)
end
end
通过深入研究 的来源,Devise::RegistrationsController
我们可以确定它调用build_resource(sign_up_params)
的内容大致相当于User.new(sign_up_params)
. 所以我们可以通过声明我们自己的sign_up_params
方法来简单地添加我们自己的参数处理。
请注意,sign_up_params
我们使用内置的 Rails 4 强大的参数处理而不是 Devise sanitized params,这是一个早于 Rails 4 的家庭滚动解决方案。可能可以使用 Devise sanitized params 来做到这一点,但除非你必须这样做,否则没有真正的理由向后兼容 Rails 3。
3.自定义注册表单
为了获得正确的参数哈希,我们希望公司名称输入具有以下name
属性:
user[company_attributes][name]
Rails 有一个很好的助手fields_for
,它可以让我们这样做:
<%# app/views/devise/registrations/new.html.erb %>
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<fieldset>
<legend>Company</legend>
<%= f.fields_for :company do |c| %>
<div class="field">
<%= c.label :name %><br />
<%= c.text_field :name%>
</div>
<% end %>
</fieldset>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
请注意,fields_for
它在块 ( ) 中为我们提供了一个新的表单构建器实例,c
我们从中创建嵌套输入。
4. 啤酒。
于 2015-07-26T09:02:45.227 回答