2012-03-28 10 views
0

初心者の質問はこちらフォーム内の関連モデルをレールに作成する方法

私は相互に関連している2つのモデルがあります:

class Relationship < ActiveRecord::Base 
    ... 
    attr_accessible :source_item_id, :target_item_id 
    belongs_to :target_item, :class_name => "Item" 
    belongs_to :source_item, :class_name => "Item" 
    belongs_to :user 
    ... 
end 

と:

class Item < ActiveRecord::Base 
    ... 
    attr_accessible :address 
    ... 
end 

は、私のフォーム内で、私はすでにsource_item_idを知っています。私はフォームにアドレスを入力して、target_itemと関連するRelationshipの両方を作成できるようにします。

<%= form_for @new_relationship do |f| %> 

    <% @new_relationship.source_item_id = @current_item.id %> 
    <%= f.hidden_field :source_item_id %>  

    <%= f.submit "New Relationship" %> 
<% end %> 

答えて

0

通常、コントローラで関係を作成してフォームにデータを収集させます。 2つのモデルでフォームを作成する方法を尋ねる場合は、hereの投稿を参照してください。私はあなたの質問を正しく理解することを願っています!

関連する問題