Ruby on rails Rails:如何从一个表单保存到两个表中?
在注册期间,我必须在另一个表中插入一些值。这就是我迄今为止所尝试的 这两个表是Ruby on rails Rails:如何从一个表单保存到两个表中?,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,在注册期间,我必须在另一个表中插入一些值。这就是我迄今为止所尝试的 这两个表是员工和组织。这是我目前使用的表格: <%= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:id => "example-advanced-form"}) do |f| %> <h3>Account</h3> <fields
员工
和组织
。这是我目前使用的表格:
<%= 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 %>
{:id=>“示例高级形式”})do | f |%>
账户
帐户信息
“电子邮件-2”,:class=>“所需电子邮件”%>
“密码-2”,:类=>“必需”%>
“确认”、:id=>“确认-2”、:class=>“必需”%>
(*)强制性
轮廓
配置文件信息
“名称-2”,:类=>“必需”%>
“姓氏-2”,:类=>“必需”%>
“姓氏-2”,:类=>“必需”%>
名称
开发商
测试员
经理
其他
“名称-2”,:类=>“必需”%>
组织业务类型
生意
财务
信息技术
其他
员工人数
0-10
10-50
50-100
100-500
500-1000
1000以上
“地址-2”,:类=>“必需”%>
“必需”%>
“必需”%>
“必需”%>
“电子邮件-2”,:class=>“所需电子邮件”%>
完成
条款和条件
我们已向您发送了一封激活您帐户的邮件。请通过邮件激活您的帐户。如果您尚未收到任何邮件,请单击
下面块中的表单项
<%= 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
class EmployeesController
您想在不同的表中保存什么?当您收到所有表单数据时,您可以在控制器中拆分表单数据。@AlexanderLuna我想在签名时保存组织的详细信息up@Flip你能帮我吗?