Ruby on rails Rails:如何迭代模型的子集';要创建输入字段的字段?

Ruby on rails Rails:如何迭代模型的子集';要创建输入字段的字段?,ruby-on-rails,ruby,views,iteration,Ruby On Rails,Ruby,Views,Iteration,我有一个模型如下: class User < ActiveRecord::Base attr_accessible :name, :email, :twitter, :dribbble, :forrst, :github, :stackoverflow, :linkedin # validations, functions, etc. end <%= form_for(@user) do |f| %> <% %w[twi

我有一个模型如下:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :twitter, :dribbble, :forrst,
                  :github, :stackoverflow, :linkedin

  # validations, functions, etc.
end
<%= form_for(@user) do |f| %>
  <% %w[twitter dribbble forrst github stackoverflow linkedin].each do |account| %>
    <%= account %> username: <%= f.text_field :account %>
  <% end %>
<% end %>
class用户
在我看来,我想对社交账户(twitter、Dribble、forrst、github、stackoverflow和linkedin)进行迭代,为它们创建如下文本字段:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :twitter, :dribbble, :forrst,
                  :github, :stackoverflow, :linkedin

  # validations, functions, etc.
end
<%= form_for(@user) do |f| %>
  <% %w[twitter dribbble forrst github stackoverflow linkedin].each do |account| %>
    <%= account %> username: <%= f.text_field :account %>
  <% end %>
<% end %>

用户名:
如何为
f.text\u字段
赋予正确的
:帐户

谢谢

试试这个:

<%= f.text_field account.to_sym %>

要保持干燥,请执行以下操作:

<%= form_for(@user) do |f| %>
  <% User.accessible_attributes.each do |account| %>
    <%= account %> username: <%= f.text_field account.to_sym %>
  <% end %>
<% end %>

用户名:

仅当未添加可访问属性时,但通常这是一个更好的主意。或者至少在用户上创建一个数组,并将其用于可访问的表单和属性。