我有一个客户端和办公室地址模型,我想在创建客户端时创建办公室地址,所以为了做到这一点,我决定采用嵌套属性方向。
当我尝试使用Office地址创建客户端时,我在服务器输出中得到了这个内容,并没有给我太多的帮助,不知道如何继续。
Started POST "/clients" for 127.0.0.1 at 2016-10-26 21:57:06 -0600
Processing by ClientsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oC4Bwgw8zQrQCGU6RVGXXVwgWGIbOGmyP9gmJYUbyKXVXzgdeRGrp/wMnsmbF6spSeNxTpcHLJx+ZceBKjHxvQ==", "client"=>{"account_id"=>"", "name"=>"Test Client", "client_type"=>"Corp", "client_ident"=>"1234567890", "office_address_attributes"=>{"client_id"=>"", "unit_number"=>"317", "street_number"=>"1717", "street_name"=>"60 st SE", "city"=>"Clagary", "prov_state"=>"Alberta", "postal_zip"=>"T2A7Y7", "country"=>"CA"}}, "commit"=>"Create Client"}
Account Load (0.1ms) SELECT "public"."accounts".* FROM "public"."accounts" WHERE "public"."accounts"."subdomain" = $1 LIMIT $2 [["subdomain", "shawnwilson"], ["LIMIT", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Rendering clients/new.html.erb within layouts/application
Rendered clients/_form.html.erb (32.8ms)
Rendered clients/new.html.erb within layouts/application (34.4ms)
Rendered shared/_signed_in_nav.html.erb (0.7ms)
Completed 200 OK in 109ms (Views: 102.0ms | ActiveRecord: 1.2ms)
因此,当我创建客户端时,我想将客户端关联到帐户,并且我想将OfficeAddress与客户端关联。
我的客户端型号
class Client < ApplicationRecord
belongs_to :account, required: true
has_one :office_address
validates :office_address, presence: true
accepts_nested_attributes_for :office_address
end
我的办公室地址模型
class OfficeAddress < ApplicationRecord
belongs_to :client, required: true
end
我的客户端控制器
class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy]
# GET /clients
# GET /clients.json
def index
@clients = Client.all
end
# GET /clients/1
# GET /clients/1.json
def show
end
# GET /clients/new
def new
@client = Client.new
@client.build_office_address
end
# GET /clients/1/edit
def edit
end
# POST /clients
# POST /clients.json
def create
@client = Client.new(client_params)
respond_to do |format|
if @client.save
format.html { redirect_to @client, notice: 'Client was successfully created.' }
format.json { render :show, status: :created, location: @client }
else
format.html { render :new }
format.json { render json: @client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /clients/1
# PATCH/PUT /clients/1.json
def update
respond_to do |format|
if @client.update(client_params)
format.html { redirect_to @client, notice: 'Client was successfully updated.' }
format.json { render :show, status: :ok, location: @client }
else
format.html { render :edit }
format.json { render json: @client.errors, status: :unprocessable_entity }
end
end
end
# DELETE /clients/1
# DELETE /clients/1.json
def destroy
@client.destroy
respond_to do |format|
format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_client
@client = Client.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_params
params.require(:client).permit(:account_id, :name, :client_type, :client_ident, office_address_attributes: [:unit_number, :street_number, :street_name, :city, :prov_state, :postal_zip, :country, :client_id])
end
end
我的表格
<%= simple_form_for(@client) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :account_id %>
<%= f.input :name %>
<%= f.input :client_type %>
<%= f.input :client_ident %>
</div>
<%= f.fields_for :office_address do |oa| %>
<%= oa.input :client_id %>
<%= oa.input :unit_number %>
<%= oa.input :street_number %>
<%= oa.input :street_name %>
<%= oa.input :city %>
<%= oa.input :prov_state %>
<%= oa.input :postal_zip %>
<%= oa.input :country %>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
非常感谢这里的任何帮助!
编辑#1 - 添加Byebug错误
(byebug) @client.errors
#<ActiveModel::Errors:0x007fb249813488 @base=#<Client id: nil, account_id: nil, name: "Test Client", client_type: "Corp", client_ident: "1234567890", created_at: nil, updated_at: nil>, @messages={}, @details={}>
(byebug)
答案 0 :(得分:1)
请按以下方式更改您的关联:
class OfficeAddress < ApplicationRecord
belongs_to :client, optional: true
end
答案 1 :(得分:1)
如果您不想关闭相关模型的验证(在某些情况下这不是理想的话),那么您应该像这样设置inverse_of
has_one :office_address, inverse_of: :client
inverse_of 真的值得了解,这篇博客解释得很好:
https://www.viget.com/articles/exploring-the-inverse-of-option-on-rails-model-associations
答案 2 :(得分:0)
我通过将update_attributes添加到客户端控制器的create方法来解决此问题。像这样:
$loop = new WP_Query(
array(
'post_type' => 'product'
)
);
if( $loop->have_posts() ) :
$data = array( "api_status" => 1, "api_message" => "success");
// First we get the image id
// $post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );
// // Then we get the image data, we will get an array with some informations
// $image = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
// // the image url is the first index of this array
// $image_url = $image[0];
$meta = array();
while ( $loop->have_posts() ) : $loop->the_post();
//get the first attachment. Not sure if this is the one you want
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
$attachment_ID = $attachments[0]->ID;
$meta[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"stock_status" => get_post_meta( get_the_ID(), '_stock_status', true ),
"price" => get_post_meta( get_the_ID(), '_price', true ),
"reguler_price" => get_post_meta( get_the_ID(), '_regular_price', true ),
"image" => basename( get_attached_file( $attachment_ID ) ),
);
endwhile;
我不是最好的解决方案,但它能让它发挥作用。