设计创建belongs_to关联的表单

时间:2017-05-07 10:12:57

标签: ruby-on-rails devise nested-forms

我需要用户能够在用户注册中创建公司:

class Company < ActiveRecord::Base
  has_many: :users
end

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :confirmable

  belongs_to :company
end

并形成:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { role: 'form' }) do |f| %>
  <div class="form-group">
    <%= f.label :company_name %>
    **** how to add company name? ****
  </div>
  <div class="form-group">
    <%= f.label :email %>
    <%= f.email_field :email, autofocus: true, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :password %>
    <%= f.password_field :password, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation, class: 'form-control' %>
  </div>
  <%= f.submit t('.sign_up', default: 'Sign up'), class: 'btn btn-primary' %>
<% end %>

我知道如何处理表单,我只是不知道如何创建表单。

谢谢!

0 个答案:

没有答案