我正在尝试为User建立一个多态地址,但是它不能正常工作。 我可以创建多态对象,但是它不能保存所有参数,而且我也不知道为什么!并且显示“缺少参数或值为空:地址”
创建并保存新用户时,不要保存地址的所有参数,仅保存“ addresseable_type和addresseable_id”。
地址模型
class Address < ApplicationRecord
belongs_to :addresseable, polymorphic: true
end
用户模型
class User < ApplicationRecord
has_one :address, :as => :addresseable, dependent: :destroy
has_secure_password
end
用户控制器
class UsersController < ApplicationController
before_action :authorize_session
before_action :set_user, only: [:show, :edit, :update]
def show
end
def new
@user = User.new
@user.build_address
end
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
if @user.build_address(address_params).save
format.html { redirect_to "/users", notice: 'Success!' }
end
else
format.html { render :new }
format.json { render json: @user.errors.full_messages, status: :unprocessable_entity }
end
end
end
def edit
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to "/users", notice: 'Success!' }
else
format.html { render :edit }
end
end
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :date_of_birth, :email, :password, :password_confirmation)
end
def address_params
params.require(:address).permit(:id, :address, :address_number, :extra_info, :neighborhood, :cep, :address_city, :address_state, :country_id, :addresseable_type, :addresseable_id, :_destroy)
end
end
用户视图
<div class="wrapper ">
<%= render 'layouts/sidebar' %>
<div class="content">
<%= render 'layouts/navbar' %>
<%= render 'layouts/flash_messages' %>
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="col-md-8" style="margin: auto">
<div class="card card-user">
<div class="card-header">
<h5 class="card-title">User</h5>
</div>
<div class="card-body">
<form>
<div class="form-row">
<div class="form-group col-md-6 required-field">
<label class="active">Name</label>
<%= f.text_field :name, required:true, class:"form-control", placeholder: ""%>
</div>
<div class="form-group col-md-6 required-field">
<label class="active">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>Bday</label>
<%= f.text_field :date_of_birth, required:true, class:"form-control datepicker", placeholder: ""%>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 required-field">
<label class="active">Pass</label>
<%= f.password_field :password, :required => true, class:"form-control", placeholder: ""%>
</div>
<div class="form-group col-md-6 required-field">
<label class="active">Pass</label>
<%= f.password_field :password_confirmation, :required => true, class:"form-control", placeholder: "" %>
</div>
</div>
<nav class="navbar navbar-light navbar_hr">
<span class="navbar-text text_hr">
ENDEREÇO PARA USUÁRIO
</span>
</nav>
<%= f.fields_for :address do |u| %>
<div class="form-row">
<div class="form-group col-md-6 required-field">
<label>Address</label>
<%= u.text_field :address, :required => true, class:"form-control", placeholder: ""%>
</div>
<div class="form-group col-md-3 required-field">
<label>Number</label>
<%= u.text_field :address_number, :required => true, class:"form-control", placeholder: "" %>
</div>
<div class="form-group col-md-3">
<label>Complement</label>
<%= u.text_field :extra_info, class:"form-control", placeholder: "" %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 required-field">
<label>Neighborhood</label>
<%= u.text_field :neighborhood, :required => true, class:"form-control", placeholder: ""%>
</div>
<div class="form-group col-md-6 required-field">
<label>cep</label>
<%= u.text_field :cep, :required => true, class:"form-control", placeholder: "" %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4 required-field">
<label>Country</label>
<%= u.collection_select(:country_id, Country.all, :id, :name) %>
</div>
<div class="form-group col-md-4required-field">
<label>State</label>
<%= u.text_field :address_state, :required => true, class:"form-control", placeholder: "" %>
</div>
<div class="form-group col-md-4">
<label>City</label>
<%= u.text_field :address_city, class:"form-control", placeholder: "" %>
</div>
</div>
<% end %>
<div class="form-row">
<div class="input-field update ml-auto mr-auto">
<%= f.button :submit, class:"btn btn-primary btn-round" do %>
<i class="fa fa-user"></i> Create
<% end %>
</div>
</div>
</form>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>