使用关联(has_one)模型中的字段和rails中的formtastic

sub*_*ime 2 ruby-on-rails associations formtastic

我搜索并尝试了很多,但我无法按照我的意愿完成它.所以这是我的问题.

我的模特是:

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
Run Code Online (Sandbox Code Playgroud)

在我看来:

<% semantic_form_for @user do |form| %>
  <%= form.inputs :login, :email, :password%>
  <% form.semantic_fields_for :profile do |profile| %>
    <%= profile.inputs %>
  <% end %>
  <%= form.buttons %>  
<% end %>
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我编辑一个人时,它会向我显示配置文件中的数据.我想,即使在创建用户时,也会显示配置文件中的字段.

非常感谢!

VP.*_*VP. 5

您应该在form.semantic_fields_for之前添加视图:

<% @user.build_profile unless @user.profile %>
Run Code Online (Sandbox Code Playgroud)

在创建User对象之后,您也可以在控制器中执行此操作.