Rails:如何迭代模型字段的子集来创建输入字段?

时间:2011-10-19 17:52:17

标签: ruby-on-rails ruby views iteration

我的模型如下:

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

  # validations, functions, etc.
end

在我看来,我想迭代社交帐户(twitter,dribbble,forrst,github,stackoverflow和linkedin)为他们创建文本字段,如下所示:

<%= 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_field提供正确的:account

谢谢

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

试试这个:

<%= f.text_field account.to_sym %>