Sudrien
2008-Apr-10 20:32 UTC
option_groups_from_collection_for_select with a ActiveRecord::Base single object
Rails documentation gives examples of using
option_groups_from_collection_for_select with two tables/objects.
How could it be done with one?
Example that almost works:
I have a form to enter address from both the United States and Canada.
It has the following:
====new.html.erb===<%= select_tag(
:state,
option_groups_from_collection_for_select(
State.countries,
:country,
:country,
"id",
"id")) %>
================
I have a migration with all the states/provinces I need.
==== 001_create_states.rb===class CreateStates < ActiveRecord::Migration
def self.up
create_table :states do |t|
t.string :name
t.string :postal
t.string :country
t.timestamps
end
State.create :name => ''Alabama'', :postal =>
''AL'', :country =>
''United States''
State.create :name => ''Alaska'', :postal =>
''AK'', :country =>
''United States''
State.create :name => ''Saskatchewan'', :postal =>
''SK'', :country =>
''Canada''
State.create :name => ''Yukon'', :postal =>
''YT'', :country =>
''Canada''
end
def self.down
drop_table :states
end
end
================
I also have a little bit defined in the model.
====state.rb===class State < ActiveRecord::Base
def self.countries
State.find(:all, :select => "country", :group =>
"country")
end
end
================
The optgroups are correctly made. However, I can''t find a combination
that will give me any actual state names in those lists (with this, I
get a single option with a number of unknown origin).
Documentation seems to say that "self.countries" should have ...
functions ... associated with .. the returned collection?
Guidance please?
-Sud.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Julian Leviston
2008-Apr-11 02:21 UTC
Re: option_groups_from_collection_for_select with a ActiveRecord::Base single object
Just use options_from_collection_for_select Julian. Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW! http://sensei.zenunit.com/ On 11/04/2008, at 6:32 AM, Sudrien wrote:> > Rails documentation gives examples of using > option_groups_from_collection_for_select with two tables/objects. > > How could it be done with one? > > > Example that almost works: > > I have a form to enter address from both the United States and Canada. > It has the following: > > ====new.html.erb===> <%= select_tag( > :state, > option_groups_from_collection_for_select( > State.countries, > :country, > :country, > "id", > "id")) %> > ================> > I have a migration with all the states/provinces I need. > > ==== 001_create_states.rb===> class CreateStates < ActiveRecord::Migration > def self.up > create_table :states do |t| > t.string :name > t.string :postal > t.string :country > t.timestamps > end > State.create :name => ''Alabama'', :postal => ''AL'', :country => > ''United States'' > State.create :name => ''Alaska'', :postal => ''AK'', :country => > ''United States'' > State.create :name => ''Saskatchewan'', :postal => ''SK'', :country => > ''Canada'' > State.create :name => ''Yukon'', :postal => ''YT'', :country => > ''Canada'' > end > > def self.down > drop_table :states > end > end > ================> > I also have a little bit defined in the model. > > ====state.rb===> class State < ActiveRecord::Base > def self.countries > State.find(:all, :select => "country", :group => "country") > end > end > ================> > > The optgroups are correctly made. However, I can''t find a combination > that will give me any actual state names in those lists (with this, I > get a single option with a number of unknown origin). > > Documentation seems to say that "self.countries" should have ... > functions ... associated with .. the returned collection? > > Guidance please? > > -Sud. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sudrien
2008-Apr-11 14:42 UTC
Re: option_groups_from_collection_for_select with a ActiveRecord::Base single object
Ok, that and some reading has gotten me to this:
====app/views/wherever/new.html.erb===<%= render(:partial =>
"states/state") %>
================
====app/views/states/_state.html.erb===<%= select_tag( :state,
options_from_collection_for_select (
State.states,
:postal,
:name
)) %>
================
====app/models/state.rb===class State < ActiveRecord::Base
def self.countries
State.find(:all, :select => "country", :group =>
"country")
end
def self.states
find(:all)
end
end
================
...It does show all the states, but I really want them in optgroups by
country. Obviously my "State.countries" isn''t currently
enough to do
that.
-Sud.
On Apr 10, 10:21 pm, Julian Leviston
<jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org>
wrote:> Just use options_from_collection_for_select
>
> Julian.
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Kyle
2008-Apr-12 17:05 UTC
Re: option_groups_from_collection_for_select with a ActiveRecord::Base single object
The only example that I''ve seen of this is in AWD Ch. 22 (p. 488 printed), and it seems to be a lot of work. The book wraps the option_groups_from_collection_for_select in <select> tags (html, not a helper). The first parameter given is the highest level array - in your case, it would essentially be [ "Canada", "US" ] - but each value in the array is another object (a class in the example) with the group name and sub-options available through attr_reader methods. It seems like a lot of work. If you want to do it the "good" way, read more of the API on option_groups_from_collection_for_select (I think that you were on the right track for what you want to do). Since you only have two groups, you could also do a little hard-coding and use helpers to get the states. -Kyle On Apr 11, 9:42 am, Sudrien <sudr...-5+SPVq0q6MJWk0Htik3J/w@public.gmane.org> wrote:> Ok, that and some reading has gotten me to this: > > ====app/views/wherever/new.html.erb===> <%= render(:partial => "states/state") %> > ================> > ====app/views/states/_state.html.erb===> <%= select_tag( :state, options_from_collection_for_select ( > State.states, > :postal, > :name > )) %> > ================> > ====app/models/state.rb===> class State < ActiveRecord::Base > def self.countries > State.find(:all, :select => "country", :group => "country") > end > def self.states > find(:all) > end > end > ================> > ...It does show all the states, but I really want them in optgroups by > country. Obviously my "State.countries" isn''t currently enough to do > that. > > -Sud. > > On Apr 10, 10:21 pm, Julian Leviston <jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote: > > > Just use options_from_collection_for_select > > > Julian.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---