多型号注册并在 Rails 4 内获得许可

Multi model sign up with clearance in Rails 4

所以我想在注册为我的应用程序的新用户时创建一个公司。我使用 https://github.com/thoughtbot/clearance 进行身份验证。

我有这两个迁移:

class CreateCompanies < ActiveRecord::Migration
  def change
    create_table :companies do |t|
      t.string :name, null: false
      t.string :email, null: false
      t.attachment :logo

      t.timestamps null: false
    end
  end
end

class CreateClearanceUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email, null: false
      t.string :encrypted_password, limit: 128, null: false
      t.string :confirmation_token, limit: 128
      t.string :remember_token, limit: 128, null: false
      t.string :first_name
      t.string :last_name
      t.attachment :avatar
      t.integer :company_id

      t.timestamps null: false
    end
    add_index :users, :email, unique: true
    add_index :users, :remember_token
  end
end

我的模型是这样的:

class User < ActiveRecord::Base
  include Clearance::User

  belongs_to :company
end
class Company < ActiveRecord::Base
  has_many :users
  accepts_nested_attributes_for :users
end

这是我修改过的 Clearance 控制器:

class Clearance::UsersController < ApplicationController
  before_filter :redirect_signed_in_users, only: [:create, :new]
  skip_before_filter :require_login, only: [:create, :new]
  skip_before_filter :authorize, only: [:create, :new]

  def new
    @user = User.new
    @user.build_company

    render template: "users/new"
  end

  def create
    @user = User.new(user_params)

    if @user.save
      sign_in @user
      redirect_back_or url_after_create
    else
      render template: "users/new"
    end
  end

  private

  def avoid_sign_in
    warn "[DEPRECATION] Clearance's `avoid_sign_in` before_filter is " +
      "deprecated. Use `redirect_signed_in_users` instead. " +
      "Be sure to update any instances of `skip_before_filter :avoid_sign_in`" +
      " or `skip_before_action :avoid_sign_in` as well"
    redirect_signed_in_users
  end

  def redirect_signed_in_users
    if signed_in?
      redirect_to Clearance.configuration.redirect_url
    end
  end

  def url_after_create
    Clearance.configuration.redirect_url
  end

  def user_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, companies_attributes: [:id, :name])
  end
end

最后是我的表格:

<div class="form-item">
  <%= form.label :first_name %>
  <%= form.text_field :first_name, type: "text" %>
</div>

<div class="form-item">
  <%= form.label :last_name %>
  <%= form.text_field :last_name, type: "text" %>
</div>

<div class="form-item">
  <%= form.label :email %>
  <%= form.text_field :email, type: "email" %>
</div>

<div class="form-item">
  <%= form.fields_for :companies do |builder| %>
    <label>Company</label>
    <%= builder.text_field :name, type: "text" %>
  <%end%>
</div>

<div class="form-item">
  <%= form.label :password %>
  <%= form.password_field :password %>
</div>

我可以完美地提交我的表单,但是在 rails 控制台中找到用户对象时,我的 company_id 为零。

有人知道我做错了什么吗?

提前致谢!

您需要使用单数:

= form.fields_for :company

并在您的控制器中:

company_attributes # instead of companies_attributes

而且你有一件事的模型不对!您想与用户一起保存公司,因此需要将 accepts_nested_attributes_for 移动到 user.rb

accepts_nested_attributes_for :company