我搜索并尝试了很多,但我无法按照我想要的方式完成它..所以这是我的问题。
我的模型是:
class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
attr_accessible :user_id, :form, :title, :name, :surname, :street, :housenumber, :zipcode, :place, :phone, :mobile, :fax, :url
belongs_to :user
end
在我看来:
<% semantic_form_for @user do |form| %>
<%= form.inputs :login, :email, :password%>
<% form.semantic_fields_for :profile do |profile| %>
<%= profile.inputs %>
<% end %>
<%= form.buttons %>
<% end %>
我的问题是,当我编辑一个人时,它会向我显示个人资料上的数据。我希望即使在创建用户时也会显示配置文件中的字段。
非常感谢!
最佳答案
您应该在 form.semantic_fields_for 之前添加 View :
<% @user.build_profile unless @user.profile %>
创建 User 对象后,您也可以在新的 Controller 中执行此操作。
关于ruby-on-rails - 在 Rails 中使用带有 formattastic 的关联 (has_one) 模型中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2526961/