我必须在注册期间将一些值插入另一个表中。这是我到目前为止所尝试过的。
这两个表格为employees
和organizations
。这是我目前使用的表单:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:id => "example-advanced-form"}) do |f| %>
<h3>Account</h3>
<fieldset>
<legend>Account Information</legend>
<%= f.label :email %><br>
<%= f.text_field :email, :id => "email-2", :class => "required email" %>
<%= f.label :password %><br>
<%= f.password_field :password, :id => "password-2", :class => "required" %>
<%= f.label :password_confirmation %><br>
<%= f.password_field :password_confirmation, :name => "confirm", :id => "confirm-2", :class => "required" %>
<p>(*) Mandatory</p>
</fieldset>
<h3>Profile</h3>
<fieldset>
<legend>Profile Information</legend>
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<%= f.label :first_name %><br>
<%= f.text_field :first_name, :id => "name-2", :class => "required" %>
<%= f.label :middle_name %><br>
<%= f.text_field :middle_name, :id => "surname-2", :class => "required" %>
<%= f.label :last_name %><br>
<%= f.text_field :last_name, :id => "surname-2", :class => "required" %>
<label>Designation</label><br>
<select class="required">
<option value="Developer">Developer</option>
<option value="Tester">Tester</option>
<option value="Manager">Manager</option>
<option value="Other">Other</option>
</select>
<br>
<%= f.fields_for :organisation do |o| %>
<%= o.label :org_name %><br>
<%= o.text_field :org_name, :id => "name-2", :class => "required" %>
<label>organisation Business Type</label><br>
<select class="required">
<option value="Business">Business</option>
<option value="Finance">Finance</option>
<option value="IT">IT</option>
<option value="Other">Other</option>
</select><br>
</div>
<div class="col-lg-6">
<label>Number Of Employees</label><br>
<select class="required">
<option value="0-10">0-10</option>
<option value="10-50">10-50</option>
<option value="50-100">50-100</option>
<option value="100-500">100-500</option>
<option value="500-1000">500-1000</option>
<option value="1000 above">1000 above</option>
</select><br>
<%= o.label :address %><br>
<%= o.text_field :address, :id => "address-2", :class => "required" %>
<%= o.label :city %><br>
<%= o.text_field :city, :class => "required" %>
<%= o.label :state %><br>
<%= o.text_field :state, :class => "required" %>
<%= o.label :country %><br>
<%= o.text_field :country, :class => "required" %>
<%= o.label :email_id %><br>
<%= o.text_field :email_id, :id => "email-2", :class => "required email" %>
<% end %>
</div>
</div>
</div>
</fieldset>
<h3>Finish</h3>
<fieldset>
<legend>Terms and Conditions</legend>
<p>We have sent you a mail for Activation of your Account. Kindly Activate your through the mail. If you haven't received any mail, please click </p>
<%= link_to "here" %>
</fieldset>
<% end %>
下面的表单项
<%= f.fields_for :organisation do |o| %>
<% end %>
在浏览器中不可见。并且值也不存储在数据库中。
以下是模型和视图控制器。
class EmployeesController < ApplicationController
before_action :authenticate_employee!
def index
@employees = Employee.all
end
def new
@employee = Employee.new
@employee.build_organisation
end
def edit
@employee = Employee.find(params[:id])
end
def create
@employee = Employee.new(employee_params)
if @employee.save
flash[:notice] = "Employee was successfully created"
redirect_to employees_path
else
flash[:notice] = "Employee was not created"
end
end
def update
@employee = Employee.find(params[:id])
if @employee.update(employee_params)
flash[:notice] = "Employee was successfully updated"
redirect_to employees_path
else
render 'new'
end
end
def destroy
@employee = Employee.find(params[:id])
@employee.destroy
redirect_to employees_path
end
private
def employee_params
params.require(:employee).permit(:employee_id, :org_id, :role_id, :first_name, :last_name, :middle_name, :dob, :gender, :email_id, :blood_group, :join_date, :left_date, :left_reason, :referred_by, :job_code, :department_code, :encrypted_password, :is_admin, :is_activated, :remarks, :status, :created_by, :updated_by,organisation_attributes: [:org_name,:country,:state,:city,:address,:email_id])
end
end
class OrganisationsController < ApplicationController
def index
@organisations = Organisation.all
end
def show
@org = Organisation.find(params[:id])
@clients = Client.where(org_id: @org.org_id)
end
end
class Employee < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :tasks
belongs_to :organisation
belongs_to :role
accepts_nested_attributes_for :organisation
validates :first_name, presence: true, length: { minimum: 5 }
validates :last_name, presence: true, length: { minimum: 5 }
end
class Organisation < ApplicationRecord
has_many :clients
has_many :employees
end