我有一个模型如下:
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
?
谢谢
最佳答案
要保持干燥,请执行以下操作:
<%= form_for(@user) do |f| %>
<% User.accessible_attributes.each do |account| %>
<%= account %> username: <%= f.text_field account.to_sym %>
<% end %>
<% end %>
关于ruby-on-rails - rails : how to iterate over subset of model's fields to create input field?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7825744/