5

मैं यह नहीं समझ सकता कि मैं एक फॉर्म कैसे स्थापित कर सकता हूं जो एक नया Study बनाएगा जबकि संबंधित StudySubject और Facility भी बनायेगा। user_id, facility_id और study_subject_id को Study ऑब्जेक्ट बनाने के लिए उपलब्ध होना चाहिए जैसा कि आप डेटाबेस संबंध मॉडल में देख सकते हैं।accepts_nested_attributes_for का उपयोग कर Rails3 में नेस्टेड ऑब्जेक्ट्स कैसे बनाएं?

Database model

यहाँ studies के लिए माइग्रेशन है। अन्य तालिकाओं में विदेशी कुंजी नहीं है।

def self.up 
create_table :studies do |t| 
    t.references :user 
    t.references :facility 
    t.references :subject 
    t.date "from" 
    t.date "till" 
    t.timestamps 
end 
add_index :studies, ["user_id", "facility_id", "subject_id"], :unique => true 
end 

मॉडल निम्नलिखित संगठनों को परिभाषित करते हैं।

# user.rb 
has_many :studies 

# subject.rb 
has_many :studies 

# facility.rb 
has_many :studies 

# study 
belongs_to :user 
belongs_to :subject 
belongs_to :facility 

प्रश्न

1) has_many और belongs_to परिभाषाओं सही हैं?
2) मैं accepts_nested_attributes_for का उपयोग कर study कैसे बना सकता हूं?
3) एक अध्ययन केवल एक उपयोगकर्ता से संबंधित होना चाहिए। क्या मुझे एसोसिएशन को स्टोर करने के लिए user_id को हर दूसरे ऑब्जेक्ट में जोड़ने की ज़रूरत है?

व्यापक शिक्षा के 2 सप्ताह बाद मैं रेल के लिए बिल्कुल नया हूं। शायद एक बेवकूफ सवाल के लिए खेद है।

+0

आरेख को आकर्षित करने के लिए किस सॉफ्टवेयर का उपयोग किया गया था? – gmile

+0

@gmile मैंने http://www.cacoo.com का उपयोग किया – JJD

उत्तर

4

हाँ। यह काम करता हैं। एक अच्छे दोस्त ने उसकी मदद की पेशकश की। यही वह है जिसे हमने स्थापित किया है।
कृपया ध्यान दें कि इस बीच मैंने StudySubject को Subject का नाम दिया।

मॉडलstudy.rb

belongs_to :student, :class_name => "User", :foreign_key => "user_id" 
belongs_to :subject 
belongs_to :university, :class_name => "Facility", :foreign_key => "facility_id" 

accepts_nested_attributes_for :subject, :university 

नियंत्रकstudies_controller.rb

def new 
    @study = Study.new 
    @study.subject = Subject.new 
    @study.university = Facility.new 
end 

def create 
    @study = Study.new(params[:study]) 
    @study.student = current_user 

    if @study.save 
    flash[:notice] = "Successfully created study." 
    redirect_to(:action => 'index') 
    else 
    render('new') 
    end 
end 

मैं और प्रमाणन के लिए devise प्राधिकरण के लिए cancan का उपयोग करें। यही कारण है कि current_user नियंत्रक में उपलब्ध है।

नए अध्ययन दृश्यnew.html.erb

<%= form_for @study, :url => { :action => "create" } do |f| %> 

    <table summary="Study form fields"> 

    <%= render :partial => "shared/study_form_fields", :locals => { :f => f } %> 

    <%= f.fields_for :subject do |builder| %> 
     <%= render :partial => "shared/subject_form_fields", :locals => { :f => builder } %> 
    <% end %> 

    <%= f.fields_for :university do |builder| %> 
     <%= render :partial => "shared/facility_form_fields", :locals => { :f => builder } %> 
    <% end %> 

    </table> 

    <p><%= f.submit "Submit" %></p> 

<% end %> 

मुझे आशा है कि यह आप कुछ समय की बचत होगी। मैंने यह महसूस करने में काफी समय बिताया कि चीजों को कैसे स्थापित किया जाना है।

संबंधित मुद्दे