【问题标题】:Formtastic nested form field not building has_one association?Formtastic嵌套表单字段未建立has_one关联?
【发布时间】:2011-08-23 16:47:19
【问题描述】:

给定一个可能是艺术家的用户:

class User < ActiveRecord::Base
  has_one :artist
end

我有一个用户和艺术家嵌套表单(使用 Formtastic gem):

<h1>Artist registration</h1>

<% @user.build_artist unless @user.artist %>

<%= semantic_form_for @user, :url => create_artist_path do |f| %>
  <%= f.inputs :username %>

  <%= f.semantic_fields_for :artist do |a| %>
    <%= a.input :bio %>
  <% end %>

  <%= f.buttons do %>
    <%= f.commit_button 'Register as Artist' %>
  <% end %>
<% end %>

问题是 :artist 字段未呈现。

我也试过f.inputs :for =&gt; :artist do |a|

由于某种原因,使用@user.build_artist 不会在表单中显示艺术家的字段。如果我尝试@user.artist = Artist.new 我会收到一个错误,因为它会尝试保存艺术家并且验证失败。

我应该如何初始化 Artist 模型,以便在嵌套形式中获得格式生成器的好处? (注意这里的@user不是:new_record?

【问题讨论】:

    标签: ruby-on-rails-3 activerecord formtastic


    【解决方案1】:

    你记得在 user.rb 中设置accepts_nested_attributes_for :artist 吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多