新手问题 当 new 或者 edit 一个对象的关联对象时 怎么写 text 标签

themorecolor · September 04, 2013 · Last by themorecolor replied at September 04, 2013 · 2127 hits
<%= form_for @user do |f| %>

    <%= f.label :username %>
    <%= f.text_field :username %>
<% end %>

如上 假如 username 属性不在 User 里 而在 user 关联的 userinfo 里面 除了虚拟属性 还有什么方法吗

用 Nested Attributes http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

class User < ActiveRecord::Base
  has_one :user_info

  accepts_nested_attributes_for :user_info
end
<%= form_for @user do |f| %>
  <%= f.fields_for :user_info do |build| %>
    <%= build.label :username %>
    <%= build.text_field :username %>
  <% end %>
<% end %>

#1 楼 @yesmeck thx 高端大气上档次

You need to Sign in before reply, if you don't have an account, please Sign up first.