2009-08-11 24 views
1

iは1ページ(2グループに属する1つのメッセージ)collection_select選択された値

<%= 
collection_select(:message,:group_ids, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'}) 
%> 
<%= 
collection_select(:message,:group_ids, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'}) 
%> 

上の2つの同一のcollection_selectsを有するcollection_selectを使用して2つの異なる選択された値を設定することが可能ですか?

編集

私は

<% 
@message.group_id=5 
%> 
<%= 
collection_select(:message,:group_id, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'}) 
%> 
<% 
@message.group_id=6 
%> 
<%= 
collection_select(:message,:group_id, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'}) 
%> 

ような何かをしたが、もちろん、それは

EDIT2を仕事とする方法行方不明のエラーを与えていない必要があるだろうと思います。

collection_selectで行うことはできません。グループにメソッドがない場合は、毎回単一のgroup_idを返します。私がなってしまったものを

は、あなたがそうのようなあなたのモデルとの関係を設定する必要があり

select_tag 'message[group_ids][]', "<option></option>"+options_from_collection_for_select(Group.find(:all), 'id', 'title',group1.id) 
select_tag 'message[group_ids][]', "<option></option>"+options_from_collection_for_select(Group.find(:all), 'id', 'title',group2.id) 
+0

最初の質問は、これを試してみる理由です。 –

+0

質問が例でしたが、実際に何をしようとしているのですか? <% @ message.groups.each do | group | %> collection_select(:message、group.id、Group.find(:all)、:id、 %> <% エンド %> メッセージがグループとHABTM関係を持っているので、それはgroup_ids方法がありますが、collection_selectは私がgroup.idを使用することができ、またはしない:選択したキーワードコメントを おかげでなく、私は別のソリューションを使用するつもりだと思う(私は質問に編集を加えた)。 –

答えて

5

です:フォームで次に

class Message < ActiveRecord::Base 
    has_many :message_groups 
    has_many :groups, :through => :message_groups 
    accepts_nested_attributes_for :message_groups #Note this here! 
end 

class Group < ActiveRecord::Base 
    has_many :message_groups 
    has_many :messages, :through => :message_groups 
end 

class MessageGroup < ActiveRecord::Base 
    belongs_to :group 
    belongs_to :message 
end 

を...

<% form_for(@message) do |f| %> 
    <%= f.error_messages %> 
    <% f.fields_for :message_groups do |g| %> 
    <p> 
     <%= g.label :group_id, "Group" %> 
     <%= g.select :group_id, Group.find(:all).collect {|g| [ g.title, g.id ] } %> 
    </p> 
    <% end %> 
    <p> 
    <%= f.submit 'Update' %> 
    </p> 
<% end %> 

そして、完全性のためのマイグレーションは

です
class CreateGroups < ActiveRecord::Migration 
    def self.up 
    create_table :groups do |t| 
     t.string :title 
     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :groups 
    end 
end 

class CreateMessages < ActiveRecord::Migration 
    def self.up 
    create_table :messages do |t| 
     t.text :body 
     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :messages 
    end 
end 


class CreateMessageGroups < ActiveRecord::Migration 
    def self.up 
    create_table :message_groups do |t| 
     t.integer :message_id 
     t.integer :group_id 
     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :message_groups 
    end 
end 

これは役に立ちます...!

+0

私にはhas_and_belongs_to_many関係のように見えます。また、 "create_table:message_groups:id => false do | t |"を読むための移行ではありません。またはあなたはファンキーなエラーを取得しますか? – askegg

+0

ひどいです。 :id => falseは、HABTMリレーションに対してのみ有効です。上記のコードは素晴らしいです。 – askegg

+0

ありがとうございますaskegg:D – Chalkers

関連する問題