0

我有一個客戶端和辦公地址模型,我想創建客戶端時創建的辦公地址,所以要做到這一點,我決定去嵌套的屬性方向。表格與嵌套屬性不保存導軌5

當我嘗試使用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

你@得到該錯誤client.errors?請從服務器日誌中添加此處,似乎由於回滾,您的對象未保存在數據庫中。 – user100693

+0

@ShefaleeChaudhary虐待抓住那儘快 –

+0

它沒有給我任何形式的錯誤..只是說請檢查下面的問題:但沒有錯誤存在? –

回答

3

請改變你的關聯按如下:

class OfficeAddress < ApplicationRecord 
    belongs_to :client, optional: true 
end 
  • Rails的5協會屬於申請驗證您的客戶端ID,所以你的條目正在回滾。
+1

做到了這一點,但它仍然沒有聯繫..我已經解決了這個問題但我的答案 –

0

我通過將update_attributes添加到客戶端控制器的create方法來解決了此問題。像這樣:

def create 
    @client = Client.new(client_params) 

    respond_to do |format| 
     if @client.save 
     ### This is what I added ### 
     @client.update_attributes!(account_id: @current_account.id) 
     @client.office_address.update_attributes!(client_id: @client.id) 

     format.html { redirect_to @client, notice: 'Client was successfully created.' } 
     format.json { render :show, status: :created, location: @client } 
     else 
     puts @client.errors 
     format.html { render :new } 
     format.json { render json: @client.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

我不是最好的解決方案,但它得到它的工作。