我有3个模型,并且我想创建一个用地址构建用户的表单。但是每次我尝试创建时,地址参数都为空。
这是有问题的代码:
user.rb
class User < ApplicationRecord
has_one :email_user
has_one :address
accepts_nested_attributes_for :address
end
address.rb
class Address < ApplicationRecord
belongs_to :user
end
email_user.rb
class EmailUser < ApplicationRecord
belongs_to :user
end
email_user_controller.rb
def new
@email_user = EmailUser.new
@email_user.build_user
end
def create
@email_user = EmailUser.new(email_user_params)
respond_to do |format|
if @email_user.save
if @email_user.build_user(user_params).save
format.html { redirect_to "/admin/email_users"}
end
else
format.html { render :new }
format.json { render json: @email_user.errors.full_messages, status: :unprocessable_entity }
end
end
end
def email_user_params
params.require(:email_user).permit(:email_user_ids, :id, :password, :password_confirmation)
end
def user_params
params.require(:user).permit(:user_ids, :id, :name, :email, address_attributes: [:id, :address, :address_city, :address_state])
end
email_users / form.html.erb
<%= form_for([:admin,@email_user]) do |e| %>
<div class="row">
<div class="col-md-8" style="margin: auto">
<div class="card card-user">
<div class="card-header">
<h5 class="card-title">Users</h5>
</div>
<div class="card-body">
<form>
<%= fields_for :user do |f|%>
<div class="form-row">
<div class="form-group col-md-6 required-field">
<label>Name</label>
<%= f.text_field :name, required:true, class:"form-control", placeholder: ""%>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12 required-field">
<label>Email</label>
<%= f.text_field :email, :required => true, class:"form-control", placeholder: "" %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 required-field">
<label>Password</label>
<%= e.password_field :password, :required => true, class:"form-control", placeholder: ""%>
</div>
<div class="form-group col-md-6 required-field">
<label>Password Confirmation</label>
<%= e.password_field :password_confirmation, :required => true, class:"form-control", placeholder: "" %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label>Address</label>
<%= f.text_field :address, class:"form-control", placeholder: ""%>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label>Address State</label>
<%= f.text_field :state, class:"form-control", placeholder: "" %>
</div>
<div class="form-group col-md-4">
<label>City</label>
<%= f.text_field :city, class:"form-control", placeholder: "" %>
</div>
</div>
<% end %>
<div class="form-row">
<div class="input-field update ml-auto mr-auto">
<%= e.button :submit, class:"btn btn-primary btn-round" do %>
<i class="fas fa-globe-americas"></i> Criar/Atualizar
<% end %>
</div>
</div>
</form>
</div>
</div>
<% end %>
有人知道怎么回事吗?