开发者

Use formtastic to assign has_one association?

开发者 https://www.devze.com 2023-02-18 05:22 出处:网络
Is there a way to use Formtastic to assign a has_one association? I\'m not talking about assigning the fields of an associated object via semantic_fields_for. I want to use the form to change the asso

Is there a way to use Formtastic to assign a has_one association? I'm not talking about assigning the fields of an associated object via semantic_fields_for. I want to use the form to change the association itself.

So suppose I have an Owner which has_one Car. I would like to be开发者_如何学编程 able to associate a different car with the owner from the owners' form. (I know how to assign an owner to a car from the car form, but ideally I'd like to be able to do it in both places.) The following does NOT work.

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 %>     

Any suggestions?

Thanks, K


Using Formtastic 1.2-stable on Rails3 I have this working.

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

Should give you a drop down selector containing all instances of Car in the database.

0

精彩评论

暂无评论...
验证码 换一张
取 消