0

私は 2 つのテーブル employee と company を持っています。deviseのsign_upアクションで従業員登録中に従業員の会社名を登録したい。従業員が登録している間に会社名を保存するためのデバイスパラメーターサニタイザーメソッドの書き方は?

4

2 に答える 2

0

秘訣は、accepts_nested_attributes_forsign_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したがって、独自のメソッドを宣言することで、独自の params 処理を簡単に追加できます。

ここではsign_up_params、Rails 4 より前のホーム ロール ソリューションである Devise のサニタイズ済みパラメーターの代わりに、ビルドされた Rails 4 の強力なパラメーター処理を使用していることに注意してください。 Rails 3 との下位互換性があります。

3. 登録フォームをカスタマイズする

正しい params ハッシュを取得するには、会社名の入力に次の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 に答える
0

sign_upフォームにカスタム情報を追加するためこれを試してください:

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

views/devise/registrations/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 に答える