2017-08-11 2 views
0

Ich versuche Modelle von has_many Beziehung zu konvertieren hier zu has_one ist mein CodeÄnderung has_many zu has_one Beziehung

Modelle

class Activity < ActiveRecord::Base 

    has_many :product_outline_attribute, dependent: :destroy 

    accepts_nested_attributes_for :product_outline_attribute 

end 

class ProductOutlineAttribute < ActiveRecord::Base 

    belongs_to :activity 

end 

Controller

class ActivitiesController < ProductOutlinesController 

    def new 
    @activity = Activity.new 
    @activity.product_outline_attributes.new() 
    render layout: false 
    end 

end 

anzeigen

new.html.haml

.panel 
    = form_for ([ @activity]), html: { class: 'ajax_form' } do |f| 

    = render partial: 'product_outlines/form_activity_section', locals: { f: f } 

form_activity_section.html.haml

= f.fields_for :product_outline_attribute do |ff_poa| 
    = ff_poa.label :depth_of_knowledge 
    = ff_poa.select :depth_of_knowledge, DEPTH_OF_KNOWLEDGE_LEVELS.map{ |k,v| [v,k] }, prompt: (ff_poa.object.depth_of_knowledge.blank? ? 'Select Level' : nil) 

früher denselben Code mit has_many Beziehung funktioniert, weil

@ activity.product_outline_attributes.new eine zurück Objekt

aber mit has_one

@ activity.product_outline_attribute ist gleich Null

@ activity.product_outline_attribute.new eine Ausnahme auslöst

es sinnvoll, nach dem Lesen Schienen

Führung macht

mir jemand dieses Problem helfen könnte, auf die Überwindung .

Antwort

2

Wenn Aktivität has_one product_outline_attribute. Sie müssen eine Instanz anhand von Codes wie dieser neu erstellen:

@activity.build_product_outline_attribute 
Verwandte Themen