haml select_tag

114 views
Skip to first unread message

Loha

unread,
Mar 10, 2015, 11:40:09 AM3/10/15
to ha...@googlegroups.com
Hi folks, i'm new to haml and can't get the select_tag working.  I adapted the following from an example but I can't get it to work.  The form has many text fields and checkboxes working.  The variable is :alertFrequency, database type string. i'm using rails 4. Any assistance appreciated.

= simple_form_for @alert do |f|
  .field
    = f.label "Alert Frequency"
    = select_tag :alertFrequency, options_for_select([["daily", "daily"], ["weekly", "weekly"], ["14 days","14days"], ["30 days","30days"],["immediate", "immediate"],["off","off"]], params[:alertFrequency])

engineerDave

unread,
Mar 10, 2015, 2:29:30 PM3/10/15
to ha...@googlegroups.com
refactor your collection options into a helper method 

def my_options
 
[
   
{id:"daily",      label: "daily"},
   
{id: "weekly",    label: "weekly"},
   
{id: "14 days",   label: "14days"},
   
{id: "30 days",   label: "30days"},
   
{id: "immediate", label: "immediate"},
   
{id: "off",       label: "off"}
 
]
end


  
Then you can use it with simple_forms helpers, value_method and label_method 


= simple_form_for @alert do |f|

 
.field= f.input :alertFrequency, label: "Alert Frequency", collection: my_options, value_method: :id, label_method: :label



Also note camel case is not rails' convention for naming 

On Tuesday, March 10, 2015 at 10:40:09 AM UTC-5, Loha wrote:
Hi folks, i'm new to haml and can't get the select_tag working.  I adapted the following from an example but I can't get it to work.  The form has many text fields and checkboxes working.  The variable is :alertFrequency, database type string. i'm using rails 4. Any assistance appreciated.

= simple_form_for @alert do |f|
  .field
    = f.label "Alert Frequency"
    = select_tag :alertFrequency, options_for_select()
Reply all
Reply to author
Forward
0 new messages