2009-08-11 11 views
1

i haben zwei identische collection_selects auf einer Seite (eine Nachricht zu 2 Gruppen gehören)collection_select ausgewählten Wert

<%= 
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][]'}) 
%> 

ist es möglich, verschiedene ausgewählte Werte zu setzen beiden für sie collection_select verwenden?

bearbeiten:

ich denke, ich, wie etwas zu tun haben würde

<% 
@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][]'}) 
%> 

aber natürlich funktioniert es nicht und gibt Fehler Methode fehlt

edit2:

denke, es gibt keine Möglichkeit, es mit collection_select zu tun. Es sei denn, die Gruppe hat eine Methode und gibt jedes Mal eine einzelne group_id zurück.

, was ich am Ende mit ist

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

Erste Frage ist, warum versuchen Sie dies? –

+0

Frage war ein Beispiel, was ich eigentlich versuche zu tun ist <% @ message.groups.each do | group | %> <% = collection_select (: Nachricht, group.id, Group.find (: alle) ,: id,: Titel, {}, {: name => 'Nachricht [group_ids] []'}) %> <% Ende %> Nachricht HABTM Beziehung mit Gruppen hat, so hat es GROUP_IDs Methode, aber collection_select mir nicht erlaubt, group.id zu verwenden, oder: ausgewähltes Schlüsselwort Vielen Dank für einen Kommentar, aber Ich denke, dass ich eine andere Lösung verwenden werde (ich fügte Edit zu Frage hinzu). –

Antwort

5

Sie benötigen Modelle und Beziehungen wie so einzurichten:

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 

Dann in Ihrem Formular ...

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

Und hier ist meine Migrationen für die Vollständigkeit

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 

Hoffe, das hilft ...!

+0

Sieht aus wie eine has_and_belongs_to_many Beziehung zu mir. Ist die Migration nicht dazu gedacht, "create_table: message_groups: id => false do | t |" zu lesen? oder Sie werden funky Fehler bekommen? – askegg

+0

Stratch das. : id => false ist nur für HABTM-Beziehungen gültig. Der obige Code ist cool. – askegg

+0

Danke askegg: D – Chalkers