Forum: Ruby on Rails form select box from association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
36c57b679019ae2b04b746f756d21966?d=identicon&s=25 Trochalakis Christos (Guest)
on 2007-03-13 15:03
(Received via mailing list)
Hello there,

I have the following models

class Bibliography < ActiveRecord::Base
  has_and_belongs_to_many :gthemes, :join_table =>
"gthemes_bibliographies"
end

class Gtheme < ActiveRecord::Base
  has_and_belongs_to_many :bibliographies, :join_table =>
"gthemes_bibliographies"
end

I want to include in the bibliography edit page a select box with all
the gthemes and i want the members of @bibliography.gthemes to be
marked as selected

I 'm using this in bibliography/_form.rhtml:

<%= select('bibliography', 'gthemes' , Gtheme.find(:all).collect{ |gt|
[ gt.name, gt.id]}, {}, {:multiple => true}) %>

but it doesn't mark the current gthemes as selected, any ideas?

Thanks in advance,
Christos
36c57b679019ae2b04b746f756d21966?d=identicon&s=25 Trochalakis Christos (Guest)
on 2007-03-13 16:47
(Received via mailing list)
On Mar 13, 4:02 pm, "Trochalakis Christos" <yati...@ideopolis.gr>
wrote:
>   has_and_belongs_to_many :bibliographies, :join_table =>
> [ gt.name, gt.id]}, {}, {:multiple => true}) %>
>
> but it doesn't mark the current gthemes as selected, any ideas?
>
> Thanks in advance,
> Christos

I also tried using the form_for helper but the problem still exists

<% form_for :bibliography, @bibliography, :url => { :action =>
"update" } do |f| %>
  Bgthemes: <%= f.select :gthemes, Gtheme.find(:all).collect {|c|
[c.name, c.id]}, {}, {:multiple => true} %>
  <%= submit_tag 'Save changes' %>
<% end %>
36c57b679019ae2b04b746f756d21966?d=identicon&s=25 Trochalakis Christos (Guest)
on 2007-03-20 19:55
(Received via mailing list)
On Mar 13, 5:46 pm, "Trochalakis Christos" <yati...@ideopolis.gr>
wrote:
> >   has_and_belongs_to_many :gthemes, :join_table =>
> > marked as selected
>
> I also tried using the form_for helper but the problem still exists
>
> <% form_for :bibliography, @bibliography, :url => { :action =>
> "update" } do |f| %>
>   Bgthemes: <%= f.select :gthemes, Gtheme.find(:all).collect {|c|
> [c.name, c.id]}, {}, {:multiple => true} %>
>   <%= submit_tag 'Save changes' %>
> <% end %>

For the record, I didn't manage to find a "magic way" to do it, but
this is a nice and working solution:

<%= select_tag 'gthemes[]',
  options_from_collection_for_select(@gthemes, 'id', 'name',
@bibliography.gtheme_ids ),
  {:multiple => true, :class => "wide"}
%>
36c57b679019ae2b04b746f756d21966?d=identicon&s=25 Trochalakis Christos (Guest)
on 2007-09-25 23:05
(Received via mailing list)
On Mar 20, 8:30 pm, "Trochalakis Christos" <yati...@ideopolis.gr>
wrote:
> For the record, I didn't manage to find a "magic way" to do it, but
> this is a nice and working solution:
>
> <%= select_tag 'gthemes[]',
>   options_from_collection_for_select(@gthemes, 'id', 'name',
> @bibliography.gtheme_ids ),
>   {:multiple => true, :class => "wide"}
> %>

@gthemes = Gtheme.find(:all)
This topic is locked and can not be replied to.