2014-07-23 20 views
1

我有一個JobDeliveryCost的形式,用戶可以添加交付成​​本。每次用戶添加一個時,都會創建一個附加字段來添加另一個字段。目前,我有一個表格,我會顯示這些字段。確保字段價值隨着更多的領域被添加軌道

-jdc_array=(@job.job_delivery_costs.any? ? [@job.job_delivery_costs,@new_delivery].flatten : [@new_delivery]) 
      -jdc_array.each do |jdc| 
       = simple_form_for [:admin, @job, @new_delivery] do |f| 

       %tr 
        %td 
        = jdc.timing 
        = f.input :timing, collection: JobDeliveryCost::TIMINGS, :include_blank => "please select" 
        %td 
        = f.input :delivery_cost_id, collection: DeliveryCost.order(:title), :label_method => :title,:value_method => :id 
        %td 
        -if jdc.new_record? 
         =f.submit "add" 
        -else 
        %td 
        = jdc.cost_per_unit 
        = f.input :cost_per_unit 
        %td 
        = jdc.quantity 
        = f.input :quantity 

而不是在每個表單條目上方顯示輸入的值,我該如何讓字段保存它們的值呢?

另外,我怎麼會顯示這個

= f.input :delivery_cost_id, collection: DeliveryCost.order(:title), :label_method => :title,:value_method => :id 

的價值,因爲它是DeliveryCost模型的子屬性?

對於額外的enfo我增加了我的控制器和相關模型

class Admin::JobDeliveryCostsController < ApplicationController 
    before_action :set_job 

    def index 
     # raise @job.inspect 

    if get_deliverable 
     @jdc_array=(@job.job_delivery_costs.any? ? [@job.job_delivery_costs,@new_delivery] : [@new_delivery]) 
     # raise @jdc_array.inspect 
     @new_delivery = @deliverable.job_delivery_costs.build 


    end 
    set_job_delivery_cost 
    end 


    def create 
    if @job 
     @job_delivery_cost = JobDeliveryCost.new(job_delivery_cost_params) 
     @job_delivery_cost.job = @job 
     if @job_delivery_cost.quantity.nil? 
     @job_delivery_cost.quantity = 1 
     end 
      # raise @job_delivery_cost.inspect 
     if @job_delivery_cost.save 
     flash[:success] = "Delivery Costs Added" 
     else 
     flash[:error] = "Delivery Costs not Added" 
     end 
    else 
     flash[:error] = "Couldn't find the Job." 
    end 
    redirect_to admin_job_job_delivery_costs_path(@job) 
    end 


    def destroy 
    set_job_delivery_cost 
    if @job.present? && @job_delivery_cost.present? 
     @job_delivery_cost.destroy 
     flash[:success] = "Job delivery cost removed" 
    else 
     flash[:error] = "Couldn't find the record" 
    end 
    redirect_to admin_job_job_products_path(@job) 
    end 

    private 

    def set_job 
    @job = Job.find_by(id: params[:job_id]) 
    end 

    def set_job_delivery_cost 
    @job_delivery_cost ||= JobDeliveryCost.find_by(id: params[:id]) 
    end 

    def job_delivery_cost_params 
    params.require(:job_delivery_cost).permit! 
    end 

    def get_deliverable 
    return @deliverable if @deliverable 
    if params[:contact_id].present? 
     @deliverable = Contact.find_by(id: params[:contact_id]) 
    elsif params[:client_id].present? 
     @deliverable = Client.find_by(id: params[:client_id]) 
    elsif params[:job_id].present? 
     @deliverable = Job.find_by(id: params[:job_id]) 
    end 
    @deliverable 
    end 

end 

delivery_cost.rb

# == Schema Information 
# 
# Table name: delivery_costs 
# 
# id   :integer   not null, primary key 
# title   :string(255) 
# unit   :string(255) 
# cost_per_unit :float 
# created_at :datetime 
# updated_at :datetime 
# 

class DeliveryCost < ActiveRecord::Base 

    UNIT_DAY='day' 
    UNIT_HOUR='hour' 
    UNIT_MILE='mile' 
    UNITS=[UNIT_DAY,UNIT_HOUR,UNIT_MILE] 

    has_many :job_delivery_costs 
    has_many :jobs, through: :job_delivery_costs 

    validates :cost_per_unit, presence: true 
    validates :unit, inclusion: UNITS 
    validates :title, presence: true 
    before_destroy :survive_if_jobs 

    private 

    def survive_if_jobs 
    jobs.empty? 
    end 
end 

JobDeliveryCost

# == Schema Information 
# 
# Table name: job_delivery_costs 
# 
# id    :integer   not null, primary key 
# job_id   :integer 
# delivery_cost_id :integer 
# cost_per_unit :float 
# quantity   :integer 
# timing   :string(255) 
# created_at  :datetime 
# updated_at  :datetime 
# 

class JobDeliveryCost < ActiveRecord::Base 
    TIMING_INSTALL='install' 
    TIMING_BREAKDOWN='breakdown' 

    TIMINGS=[TIMING_INSTALL,TIMING_BREAKDOWN] 

    belongs_to :delivery_cost 
    belongs_to :job 
    validates :quantity, presence: true, numericality: {greater_than_or_equal_to:1} 
    validates :timing, inclusion: TIMINGS 
    #validates :cost_per_unit, presence: true 

validate :validate_cost_per_unit 
    validate :check_associates 
    # validate :quantity_default 
    before_save :init 


    private 

    def check_associates 
    associated_object_exists DeliveryCost, :delivery_cost_id 
    associated_object_exists Job, :job_id 
    end 

    def validate_cost_per_unit 
    if delivery_cost and cost_per_unit.blank? 
     self.cost_per_unit=delivery_cost.cost_per_unit 
    end 
    return false if cost_per_unit.blank? 
    end 

    def init 
     if self.quantity.nil? 
     self.quantity = 1 
     end 
    end 

end 

回答

0

如果我理解正確的,你會想使用JavaScript解決您的問題。有一個很好的railscast,討論嵌套的模型形式,但它也展示瞭如何構建JavaScript動態添加鏈接領域的元素。

而不是鏈接,你很可能會想要註冊一個onChange事件(意味着用戶更改了字段中的數據)以添加另一個字段。