2015-12-30 59 views
1

我正在Rails 4中製作應用程序。我使用簡單表單。Rails - 單個屬性的多個條目

我有一個配置文件模型和資格模型。

的關聯是:

profile.rb

belongs_to :profile 

qualifications.rb

has_many :qualifications 

我有我的個人資料視圖的形式,包括從形式的一部分,我資格審查。

型材#形式

<%= simple_form_for(@profile) do |f| %> 
      <%= f.error_notification %> 

       <div class="form-inputs"> 

      <div class="row"> 

      <div class="intpol2"> 
       Your professional qualifications 
      </div> 

       <%= render 'qualifications/form', f: f %>  

      </div> 

資格#形式

<%= simple_fields_for :qualification do |f| %> 

    <div class="form-inputs"> 


    <div class="row"> 
     <div class="col-md-6"> 
      <%= f.input :title, :label => "Your award" %> 
     </div> 

     <div class="col-md-6"> 


     </div> 


    </div> 

    <div class="row"> 
     <div class="col-md-6"> 
      <%= f.input :level, collection: [ "Bachelor's degree", "Master's degree", "Ph.D", "Post Doctoral award"] %> 
     </div> 


     <div class="col-md-6"> 
     <%= f.input :year_earned, :label => "When did you graduate?", collection: (Date.today.year - 50)..(Date.today.year) %> 
     </div> 

    </div> 

用戶可能有一個以上的程度。我想添加一個字段,它是一個說'添加另一個資格'的按鈕,然後一組新的資格表單字段可用。

我發現這篇文章試圖做一些稍微不同的事情。我不想要10個空白的表單字段(它會使表單看起來太長)。

Creating multiple records for a model in a single view in Rails

有另一種方式來實現這一目標?

回答

0

您將尋找一顆名爲cocoon的寶石;你也可以觀看this Railscast (Nested forms),這是令人遺憾的過時,但仍然很好地解釋了結構。


的模式很簡單,但需要一些額外的部件:

  1. 有一個Ajax按鈕,呼叫控制器
  2. 控制器需要返回一個形式,並內置fields_for
  3. 您將使用JS將新的fields_for添加到原始表單

最大的問題是你的新fields_forid - 這種模式的新的實現使用child_index: Time.now.to_i

我寫這個here

這裏有一個新的版本:


阿賈克斯

首先,你需要一個「添加資格」按鈕,通過Ajax鏈接到你的控制器:

#app/views/profiles/_form.html.erb 
<%= simple_form_for(@profile) do |f| %> 
    <%= f.error_notification %> 
    <div class="form-inputs"> 
     <div class="row"> 
     <div class="intpol2">Your professional qualifications</div> 
     <%= render 'qualifications/form', f: f %>  
     </div> 
    </div> 
     <%= button_to "+", new_profile_path, method: :get %> 
<% end %> 

控制器

這將通過控制器的方法,這是我們應該能夠設法返回Ajax請求的具體迴應:

#app/controllers/profiles_controller.rb 
class ProfilesController < ApplicationController 
    respond_to :js, :html, only: :new 
    def new 
     @profile = Profile.new 
     @profile.qualifications.build 
     respond_with @profile #-> will invoke app/views/profiles/new.js.erb 
    end 
end 

響應

一旦new.js.erb有我們需要構建新的HTML表單,提取fields_for並將其附加到您的視圖中:

#app/views/profiles/new.js.erb 
var fields_for = $("<%=j render "profiles/form" %>").html(); //-> might need tweaking 
$(fields_for).appendTo("#new_profile.form-inputs"); 

CHILD_INDEX

你也應該改變你的qualifications/form到包括child_index

#app/views/qualifications/form.html.erb 
<%= simple_fields_for :qualifications, child_index: Time.now.to_i do ... 

子索引是爲了表示fields_for元素的index。在我們的情況下(因爲我們正在製作所有新記錄),這並不重要。使用Time.now.to_i確保每次都完全獨特的id


最後,你需要確保你打電話:

<%= simple_fields_for :qualifications ... %> 

...

+0

嗨豐富,非常感謝這一點。這一切都非常清楚和有益,並且很有意義。在使用繭之前。我試圖按照你的步驟。我遇到的問題是我想要編輯現有的配置文件以添加新的限定,我在<%= button_to「+」,new_profile_path,method :: get%>中出現錯誤。如果我改變它來更新,那麼是否需要在更新字段中重複控制器操作,或者是否知道在新操作中查找它? – Mel

+0

我已經完成了上述步驟並嘗試使用cocoon,但是這樣做:<%= link_to_add_association'添加資格',f:f,部分:'qualifications/qualification_fields'%> 給出了一個未定義的方法'object' :0x007fe70a4b38d8>。我不知道這個錯誤是什麼意思,但會提出一個新的問題來尋求幫助解釋它。 – Mel

+0

你能給我一個github回購的鏈接嗎?我會看看它如果沒關係 –

0

看來你必須使用嵌套形式。你必須嘗試你的鏈接教程,因爲我也會使用它。對於另一個教程,您可以將其用作參考nested_forms-rails-4.2

我希望這對你有所幫助。