遵循DRY规则,我已在我的officers\_form.html.erb
视图中插入了渲染部分命令:
<%= form_for(officer) do |f| %>
<% if officer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(officer.errors.count, "error") %> prohibited this officer from being saved:</h2>
<ul>
<% officer.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= render :partial => 'users/form', :locals => {:user => @officer.user} %>
<%= render :partial => 'addresses/form', :locals => {:address => @officer.address} %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是我的users\_form.html.erb
文件:
<%= form_for(user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :user do |user_fields| %>
<div class="field">
<%= user_fields.label :last_name %>
<%= user_fields.text_field :last_name %>
</div>
<div class="field">
<%= user_fields.label :first_name %>
<%= user_fields.text_field :first_name %>
</div>
<div class="field">
<%= user_fields.label :middle_name %>
<%= user_fields.text_field :middle_name %>
</div>
<div class="field">
<%= user_fields.label :gender %>
<%= user_fields.select(:gender, User.genders.keys) %>
</div>
<% end %>
<!--div class="actions"-->
<!--%= f.submit %-->
<!--/div-->
<% end %>
与用户代码相同的推理适用于地址代码,因此我将在此省略。
这是我的officers_controller
文件:
class OfficersController < BaseController
before_action :set_officer, only: [:show, :edit, :update, :destroy]
# GET /officers
# GET /officers.json
def index
@officers = Officer.all
end
# GET /officers/1
# GET /officers/1.json
def show
end
# GET /officers/new
def new
@officer = Officer.new
end
# GET /officers/1/edit
def edit
end
# POST /officers
# POST /officers.json
def create
@officer = Officer.new(officer_params)
respond_to do |format|
if @officer.save
format.html { redirect_to @officer, notice: 'Officer was successfully created.' }
format.json { render :show, status: :created, location: @officer }
else
format.html { render :new }
format.json { render json: @officer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /officers/1
# PATCH/PUT /officers/1.json
def update
respond_to do |format|
if @officer.update(officer_params)
format.html { redirect_to @officer, notice: 'Officer was successfully updated.' }
format.json { render :show, status: :ok, location: @officer }
else
format.html { render :edit }
format.json { render json: @officer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /officers/1
# DELETE /officers/1.json
def destroy
@officer.destroy
respond_to do |format|
format.html { redirect_to officers_url, notice: 'Officer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_officer
@officer = Officer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def officer_params
#params.fetch(:officer, {})
params.require(:officer).permit!
end
end
现在,如果我转到http://localhost:3000/officers/new
,会显示用户和地址表单中包含的部分,但是当我按下Create officer
按钮时,没有任何反应。错误在哪里?
class Officer < ApplicationRecord
belongs_to :manager#, inverse_of: :officer
has_many :customers#, inverse_of: :officer
has_one :user, as: :userable, dependent: :destroy
has_one :address, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :user, :address
end
class Manager < ApplicationRecord
has_many :officers#, inverse_of: :manager
has_one :user, as: :userable, dependent: :destroy
has_one :address, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :user, :address
end
class User < ApplicationRecord
enum gender: { female: 0, male: 1, undefined: 2 }
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :userable, polymorphic: true
end
谢谢, FZ
答案 0 :(得分:1)
您还没有在officer_params中设置user_attributes,请执行以下操作:
def officer_params
#params.fetch(:officer, {})
params.require(:officer).permit(:id, user_attributes: [:id, :last_name, :middle_name, :first_name, :gender, :_destroy])
end
并改变accepts_nested_attributes_for :user, :address
到
'accepts_nested_attributes_for :user, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :address, reject_if: :all_blank, allow_destroy: true'
你需要将address_attributes发送给你的官员参数,但由于我不知道你的数据库字段,我不能为你做那部分,但它与user_attributes几乎相同,但有不同的字段(除了:id和:_destroy对所有人都是一样的。)
编辑:
这是一种嵌套形式:
<%= form_for(officer) do |f %>
<%= f.fields_for :user do |user| %>
<%= user.text_field :last_name %>
<%= user.text_field :middle_name %>
<%= user.text_field :first_name %>
<% end %>
<%= f.fields_for :address do |address| %>
<%= address.text_field :street_name %>
<%= address.text_field :zip_code %>
<% end %>
<%= f.submit 'submit' %>
这样一个提交按钮也可以为所有嵌套表单提供。
你拥有的是:
<%= form_for(officer) do |f %>
<%= form_for(user) do |f|
<%= f.fields_for :user do |user| %> // this (f) now stands for the user form instead of the officer form
<%= user.text_field :last_name %>
<%= user.text_field :middle_name %>
<%= user.text_field :first_name %>
<% end %>
<% end %>
<%= form_for(address) do |f| %>
<%= f.fields_for :address do |address| %> // same for this one
<%= address.text_field :street_name %>
<%= address.text_field :zip_code %>
<% end %>
<% end %>
<%= f.submit 'submit' %>
现在您没有嵌套表单,只有3种不同的完整表单,并且您无法通过这种方式提交多个表单和一个提交按钮。