Miguel.camba
2010-Jun-17 11:17 UTC
Can i use a hash on a collection_select? If don''t, alternatives.
In my current application, i have food categories: mexican, home made,
japanese, ect
As these categories only have ID and name, no other attributes, i dont
want to create a table on the DB.
My first idea was to create a constant named FOOD_CATEGORIES, that is
has like:
FOOD_CATEGORIES = { 1=>''mexican'',
2=>''japanese'', ... }
BUT, since the hash doesn''t have :id method, i dont know how to use
this constant on a collection_select in my views.
Do you have a better idea?
Thanks
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Jun-17 12:29 UTC
Re: Can i use a hash on a collection_select? If don''t, alternatives.
On 17 June 2010 12:17, Miguel.camba <miguel.camba-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In my current application, i have food categories: mexican, home made, > japanese, ect > As these categories only have ID and name, no other attributes, i dont > want to create a table on the DB. > My first idea was to create a constant named FOOD_CATEGORIES, that is > has like: > FOOD_CATEGORIES = { 1=>''mexican'', > 2=>''japanese'', ... } > BUT, since the hash doesn''t have :id method, i dont know how to use > this constant on a collection_select in my views. > > Do you have a better idea?Maybe options_for_select is what you are looking for. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Miguel.camba
2010-Jun-17 15:40 UTC
Re: Can i use a hash on a collection_select? If don''t, alternatives.
I dont think so. I want to show a select that shows que values of the hash but the stored data when you submit a form is the key of that value un the hash. On Jun 17, 2:29 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 17 June 2010 12:17, Miguel.camba <miguel.ca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > In my current application, i have food categories: mexican, home made, > > japanese, ect > > As these categories only have ID and name, no other attributes, i dont > > want to create a table on the DB. > > My first idea was to create a constant named FOOD_CATEGORIES, that is > > has like: > > FOOD_CATEGORIES = { 1=>''mexican'', > > 2=>''japanese'', ... } > > BUT, since the hash doesn''t have :id method, i dont know how to use > > this constant on a collection_select in my views. > > > Do you have a better idea? > > Maybe options_for_select is what you are looking for. > > Colin-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Miguel.camba
2010-Jun-17 16:17 UTC
Re: Can i use a hash on a collection_select? If don''t, alternatives.
I have just found a solution, but using a helper
In application_helper i wrote this function:
def select_from_hash(object, name, arg)
select = "<select id=\"#{object}_#{name}\"
name=\"#{object}
[#{name}]\""
arg.each do |i,e|
select += "<option
value=\"#{i}\">#{e}</option>"
end
select += "</select>"
end
And in my views I use it this way (im using Haml):
.field= render :inline=> select_from_hash(:profile, :loved_food,
CATEGORIES)
Works fine but im all ears for other-more-elegant approach.
On Jun 17, 5:40 pm, "Miguel.camba"
<miguel.ca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I dont think so. I want to show a select that shows que values of the
> hash but the stored data when you submit a form is the key of that
> value un the hash.
>
> On Jun 17, 2:29 pm, Colin Law
<clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:
>
>
>
> > On 17 June 2010 12:17, Miguel.camba
<miguel.ca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > > In my current application, i have food categories: mexican, home
made,
> > > japanese, ect
> > > As these categories only have ID and name, no other attributes, i
dont
> > > want to create a table on the DB.
> > > My first idea was to create a constant named FOOD_CATEGORIES,
that is
> > > has like:
> > > FOOD_CATEGORIES = { 1=>''mexican'',
> > >
2=>''japanese'', ... }
> > > BUT, since the hash doesn''t have :id method, i dont know
how to use
> > > this constant on a collection_select in my views.
>
> > > Do you have a better idea?
>
> > Maybe options_for_select is what you are looking for.
>
> > Colin
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.