![](/img/trans.png)
[英]accepts_nested_attributes_for to link to existing record, not create a new one
[英]Use accepts_nested_attributes_for to create new records or update existing
閱讀大更新以獲取最新信息。
大家好,
我在一個涉及三個表的 rails 應用程序中有一個多對多關系:一個用戶表、一個興趣表和一個連接 user_interests 表,該表也有一個評級值,因此用戶可以對他們的每個興趣進行評級1-10 級。
我基本上是在尋找一種方法,讓新用戶在注冊時創建他們的評級,並在未來的日期編輯他們的任何個人資料信息。
我試圖用 has_many跟蹤這個問題Rails 嵌套表單:通過,如何編輯連接模型的屬性? 但我遇到的問題是嘗試將一個選擇列表合並到組合中,並且有多種興趣可以為用戶評分。
型號代碼:
user.rb
has_many :user_interests, :dependent => :destroy
has_many :interests, :through => :user_interests, :foreign_key => :user_id
accepts_nested_attributes_for :user_interests
interest.rb
has_many :user_interests, :dependent => :destroy
has_many :users, :through => :user_interests, :foreign_key => :interest_id, :dependent => :destroy
user_interest.rb
belongs_to :user
belongs_to :interest
查看代碼:
app/views/user/_form.html.erb
<%= form_for(@user) do |form| %>
... user fields
<%= form.fields_for :user_interests do |ui_form| %>
... loop through ALL interests
<% Interest.all.each do |interest| %>
<%= ui_form.select :rating, options_for_select(1..10) %>
<%= ui_form.hidden_field :interest_id, :value => interest.id %>
<% end %>
<% end %>
<% end %>
我還在我的控制器@user.interests.build.build_interest
的 new/edit 操作中包含了以下@user.interests.build.build_interest
我遇到的問題是,當我想要多個利率時,params 哈希中只傳遞了一個利率。 我也收到了 rails 拋出的異常
Interest(#2172840620) expected, got Array(#2148226700)
我錯過或弄錯了什么導致問題的小細節?
編輯:
我找到了一種強制它工作的方法,但它需要在 chrome 開發人員工具中手動編輯 HTML,我的表單元素的 :name 屬性是作為user[user_interests_attributes][rating]
生成的,但是如果我將其更改為user[user_interests_attributes][][rating]
當我更新記錄時它會起作用。 但是,我無法手動指定綁定到表單對象的表單元素的名稱。 那么我能做些什么來表明多個利率評級被通過,而不是rails 認為的一個?
大更新:
我得到了一個半功能版本,有一些細微的變化:
查看代碼:
<% form.fields_for :user_interests do |ui_form| %>
<p>
<%= ui_form.select :rating, options_for_select(1..5), :selected => :rating %>
<%= ui_form.label :interest_title %>
<%= ui_form.hidden_field :interest_id %>
</p>
<% end %>
控制器代碼:
def new
@user = User.new
Interest.all.each { |int| @user.user_interests.build({ :interest_id => int.id }) }
end
def edit
@user = @current_user
Interest.unrated_by_user_id(@user.id).each { |int| @user.user_interests.build({ :interest_id => int.id }) }
end
現在,如果不存在評級,我可以編輯並更新或創建我的 user_interests,但是當我嘗試創建新用戶時出現用戶為空的錯誤。 此外,我無法訪問表單中的任何興趣屬性來顯示用戶實際評分的興趣。 任何人都可以幫助解決這些警告嗎?
你只需要@user.interests.build
因為它是一個 has_many 關系。 build_interest
用於 has_one/belongs_to 關系。
使用fields_for :user_interests
您是在告訴 User 模型,當創建/更新用戶時,一個或多個 user_interest 對象的實例將位於參數哈希中。 該表單不會創建或更新任何 user_interests,但它會發回一個 user_interest_attributes 哈希數組,這些哈希代表表單引用的用戶的 user_interests。 這是一個 user_interests 評級值數組,當您在表單中引用它們時不存在 user_interests 評級值,這就是您收到錯誤的原因。
由於您將范圍傳遞給select
表單助手,因此您實際上並未為表單提供任何興趣以供選擇。 select 將為 user_interests 表中的 rating 列設置一個值,其值介於 1 和 10 之間。即使 user_interests 表具有 rating 列,也不存在要設置的評級的 user_interest 。
在選擇標簽的選項哈希中傳遞:multiple => true
將創建一個多選列表,但我認為這不是您想要的。 我認為您希望用戶可以對頁面上的許多項目進行評分。
如果您確實希望用戶能夠選擇許多興趣,這就是在has_many :through
關系上使用 fields_for 和 accepts_nested_attributes_for 的方法:
<%= form_for(@user) do |f| %>
<% f.fields_for :interest_ids do |interest| %>
<ul>
<% Interest.all.each do |choice,i| %>
<li class="selection">
<%= interest.check_box [], { :checked => f.object.user_interest_ids.include?(choice.id) }, choice.id, '' %>
<%= interest.label [], choice.name %>
</li>
<% end %>
</ul>
<% end %>
<% end %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.