2017-03-06 95 views
0

我很難理解如何將現有對象(類型爲person)添加到Ruby-on-Rails中的另一個表(事件)。我創建了表格人員和表格事件之間的多對多關係,但現在我不知道如何將現有人員添加到現有事件。這是我的模型:將現有人員添加到rails中的事件

人:

class Person < ApplicationRecord 
has_many :messages 
has_and_belongs_to_many :event 

validates :name, presence: true 
validates :email, presence: true 
validates :birth, presence: true 
end 

事件:

<%= form_for([@event, @event.people.build]) do |f| %> 
    <p> 
     <%= f.label :email %><br> 
     <%= f.text_field :email %> 
    </p> 
    <p> 
     <%= f.submit %> 
    </p> 
<%end%> 

class Event < ApplicationRecord 
has_many :messages 
has_and_belongs_to_many :people 

validates :title, presence: true 
validates :description, presence: true 
validates :minimum_age, presence: true 
validates :maximum_age, presence: true 
validates_numericality_of :maximum_age, :greater_than => :minimum_age 
validates :start, presence: true 
validates :end, presence: true 
end 

現在,我使用的是官方軌教程製作這種形式在事件的看法很明顯,form_for-parameters是錯誤的,但我真的不知道它們應該是什麼?我只想讓用戶填寫他的電子郵件地址(對應於現有人員)並將用戶註冊爲此事件的服務員。我如何做到這一點?

回答

0

我相信你的問題有點寬泛,但我會盡力在一般情況下幫助你。

要添加到personevent可能想:

  1. 設置新person觀點與形式。 Rails默認生成的視圖會做。

  2. 在您的表格中使用select_tag助手,所有禮物的列表events及其ID。

  3. 隨着那個當你創建新的person你需要從選擇框中選擇event。當您選擇並提交表單時,Rails會將所選事件的ID存儲到event_id屬性person對象中,並將其放入數據庫中。

  4. 確保您已將強有力參數添加到所需的所有屬性。

我的技巧很寬泛,他們需要一些從您的谷歌搜索。但沒有你當前設置的更多細節,這是我所能做到的。

相關問題