class User
has_one :user_profile
end
class UserProfile
belongs_to :user
end
我正在渲染一個窗體,用於使用partials進行編輯。爲什麼<input>元素ID以嵌套形式重複?
的意見是:
user_profile/edit.html.erb
--------------------------
<%= render 'form' %>
user_profile/_form.html.erb
---------------------------
<%= form_for @user_profile do |f| %>
<%= f.fields_for @user_profile do |builder| %>
<%= render :partial => 'user_profiles/fields', :locals => { :f => builder } %>
# id is correct i.e. [user_profile][last_name] for the field below
<%= f.text_field :last_name %>
<% end %>
<%= f.submit %>
<% end %>
user_profile/_fields.html.erb
-----------------------------
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
# id's for the above fields are rendered as:
# user_profile[user_profile][first_name]
# notice the second repeated [user_profile]
如何解決它,這樣的ID的問世爲user_profile[first_name]
而不是user_profile[user_profile][first_name]
?
感謝您的解釋。我會重新考慮我在我的應用中如何使用partials。 – Zabba 2010-12-09 22:55:50