【问题标题】:Use formtastic to assign has_one association?使用 formtastic 分配 has_one 关联?
【发布时间】:2011-03-18 17:33:14
【问题描述】:

有没有办法使用 Formtastic 来分配 has_one 关联?我不是在谈论通过semantic_fields_for 分配关联对象的字段。我想使用表单来更改关联本身。

所以假设我有一个拥有一辆车的所有者。我希望能够将不同的汽车与所有者的表格相关联。 (我知道如何从汽车表格中为汽车分配车主,但理想情况下,我希望能够在这两个地方都这样做。)以下不起作用。

class Owner < ActiveRecord::Base      
  attr_accessor :car      
  has_one :car 

class Car < ActiveRecord::Base  
  belongs_to :owner

<% @owner.build_car unless @owner.car %>      
  <%= semantic_form_for(@owner) do |f| %>  
    <%= f.input :car, :as => :select, :collection => Car.all %>     

有什么建议吗?

谢谢, 克

【问题讨论】:

    标签: ruby-on-rails formtastic


    【解决方案1】:

    在 Rails3 上使用 Formtastic 1.2-stable 我有这个工作。

    <%= semantic_form_for @owner do |f| %>
      <%= f.inputs do %>
          <%= f.input :category %>
        <% end %>
    <% end %>
    

    应该给你一个下拉选择器,其中包含数据库中的所有 Car 实例。

    【讨论】:

    • 这对我不起作用。也许我使用了错误版本的 Formtastic。 (我使用的是 1.2.0。)如何安装 1.2-stable?在我的 Gemfile 中指定它不起作用。
    • 在你的 Gemfile 中输入gem 'formtastic', :git =&gt; 'git://github.com/justinfrench/formtastic.git', :branch =&gt; '1.2-stable'
    猜你喜欢
    • 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
    相关资源
    最近更新 更多