2015-04-15 52 views
1

我想創建恰好是模型的估算表單。這種形式有 客戶信息,這也是一個模型,車輛信息是另一種模式嵌入客戶的資源。員工姓名(也是模型)。我希望能夠選擇使用客戶的選擇框,這使我能夠列出客戶車輛列表,然後從模型員工列表中填充執行估算的員工。 (我正在學習ROR,所以請給我道歉,可能沒有正確的行話來解釋:我也能夠爲客戶生成選擇框,但不能超過這個選擇框,換句話說,甚至沒有能力。訪問車輛上selet箱每個客戶請幫我想使用其他模型的外鍵填充模型(使用選擇框)

型號:

class Estimate < ActiveRecord::Base 
    belongs_to :customer 
    belongs_to :vehicle 
    belongs_to :employee 
    has_many :lines_in_estimates 
end 

class Customer < ActiveRecord::Base 
    has_many :vehicles 
    has_many :estimates 
    validates :first_name, presence: true 
    validates :last_name, presence: true 
    validates :h_phone, presence: true 
    validates :c_phone, presence: true 
    validates_format_of :email, with: /\A[\w]([^@\s,;]+)@(([\w-]+\.)+   (com|edu|org|net|gov|mil|biz|info))\z/i 

    def first_name_and_last_name 
    "#{first_name} #{last_name}" 
    end 
end 

class Vehicle < ActiveRecord::Base 
    belongs_to :customer 
end 

class Employee < ActiveRecord::Base 
end 

控制器:

class EstimatesController < ApplicationController 
    before_action :set_estimate, only: [:show, :edit, :update, :destroy] 
    before_action :set_customer, only: [:show, :edit, :update, :destroy] 


    def index 
     @estimates = Estimate.all 
    end 

    def show 
    end 

    def new 
     @estimate = Estimate.new 
    end 




    private 

     def set_estimate 
      @estimate = Estimate.find(params[:id]) 
     end 

     def set_customer 
      @estimate = Estimate.find(params[:id]) 
     end 

     def set_vehicle 
      @vehicle = @customer.vehicles.find(params[:id]) 
     end 

     def estimate_params 
      params.require(:estimate).permit(:statetax, :muntax, :subtotal,  :total, :created_at, :updated_at, 
             :customer_id, :vehicle_id,  :employee_id) 
    end 

     def customer_params 
      params.require(:customer).permit(:first_name, :last_name, :addr1,  :addr2, :city, :state, :zip, :h_phone, :c_phone, :email) 
    end 
end 

形式:

<%= form_for(@estimate) do |f| %> 

    <% if @estimate.errors.any? %> 
     <div id="error_explanation"> 
     <h2><%= pluralize(@estimate.errors.count, "error") %> prohibited this  employee from being saved:</h2> 

     <ul> 
     <% @estimate.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 




    <div class="field"> 
    <%= f.label :Choose_Customer %><br> 
    <%= collection_select(:estimate, :customer_id, Customer.all, :id,  :first_name_and_last_name, prompt: true) %> 
    </div> 


    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
+0

很難確切地說出你的問題。你能給出一個特定的錯誤消息,這阻止你繼續在你的代碼?聽起來像你有一個非常複雜的領域模型。我會退後一步,弄清楚你需要什麼,然後嘗試並確定你的問題的範圍。我認爲現在它的立場太過寬泛。 –

+0

感謝您伸出援助之手。我正在嘗試做出估計功能。我需要能夠創建一個估算模型的新估算值(我需要參考客戶,他們的車輛以及準備估算的員工。這些信息是坐在其他模型上的,我希望給員工有能力通過選擇姓名進行估算,當然還有一個列表(客戶的車輛將列入清單)當然,估算需要我輸入準備估算的員工。 –

回答

0

如果我正確理解您的用例,那麼客戶有很多車輛,並且車輛有很多估計值。通過正確設置Rails模型,您可以撥打customer.vehicles返回該客戶擁有的一系列車輛。迭代完成後,您可以撥打vehicle.estimates獲得每輛車的估計數。

class Customer << ActiveRecord::Base 
    has_many :vehicles 
end 

class Vehicle << ActiveRecord::Base 
    belongs_to :customer 
    has_many :estimates 
end 

class Estimate << ActiveRecord::Base 
    belongs_to :vehicle 
end 

在您的車輛和估計CREATE_TABLE遷移,您需要分別和t.belongs_to :customer所以t.belongs_to :vehicle外鍵在你的數據庫,讓Rails的進行連接建立。

使用has_many時命名您的表時使用複數名稱。

一旦這個設置和工作,爲了得到客戶的所有估計,我認爲你所需要做的就是將has_many :estimates, through :vehicles添加到你的客戶模型。這將使customer.estimates返回所有估計。儘管如此,其餘的工作仍然首先進行,但我對這裏的工作還不太確定。

一些光讀在這here

相關問題