SINATRA + HAML + Select tag

1,543 views
Skip to first unread message

wea_gruena

unread,
Jul 14, 2010, 4:02:28 AM7/14/10
to Haml
Hallo,
I'm new with HAML, a so called newbie ;-)

I have a problem with HAML an SINATRA inside a form.
I want to use a select tag for display / selection of informations:

%tr
%td
%label{:for => "pmodule"} Start with PDMS module:
%td
%select{:name => "pmodule"}
- for pm in @pm
%option{:selected => @pr.pmodule_name, :include_blank =>
true} #{pm.name}

For Rails I've found informations like here (http://www.codeweblog.com/
rails-use-of-select/).

But for pure Ruby / Sinatra I've found nothing.

Can anybody help me ?

Thanks,
Andreas

Raving Genius

unread,
Jul 14, 2010, 1:17:12 PM7/14/10
to Haml
It seems like you're confusing a Rails' helper method (option_tag)
with regular HAML (%option). %option{}... is just telling HAML to
generate HTML (ie <option include_blank="include_blank">...</option>).
include_blank is not a valid HTML attribute, and even if it was, it
would belong to the select element, not option. :include_blank is an
option that some other Rails' helpers use to generate a blank option
element before generating the other option elements. I hope that helps
you understand what you posted.

You have some potential solutions:

* If you only need this in one or two places, just write it out.
Creating a partial/helper just isn't worth the effort.
* Create your own partials/helpers to use. If you only need a small
number of partials/helpers, I would recommend this.
* Figure out a way to incorporate ActionView so you can use Rails'
helpers. This would probably be more trouble than it is worth, but if
it is worth the trouble, why not upgrade your application to Rails?

PS Ruby has a better looping syntax. Use @pm.each do |pm|

wea_gruena

unread,
Jul 16, 2010, 6:03:12 AM7/16/10
to Haml
Hi,
thanks a lot for your reply.

I understand your comments about mixing of rails and haml code, but
"include_blank" isn't my big problem.

My problem is the "select mechanism", that means if I select an item
and save, than this will be stored in the database (that's O.K.).
But if I open the form again, the stored item will not pre-selected.
Where is my mistake or how can I solve this ?

Raving Genius

unread,
Jul 16, 2010, 2:03:59 PM7/16/10
to Haml
You need to tell whatever generates your list of option tags which one
was selected. One way to do this is by passing a variable containing
the selected value and checking for that value when generating your
options. For instance, using a custom partial helper method:

# app.rb
helpers do
def partial(name, locals = {}, options = {})
options[:layout] = :none
options[:locals] ||= {}
options[:locals].merge! locals
haml name.to_sym, options
end
end

# select.haml
%select{ :id => '', :name => '' }
- if include_blank
%option= include_blank
- options_list.each do |option|
- value, text = option
%option{ :value => value, :selected => (value == selected_value) }
= text

# form.haml
= parital :select, :include_blank => 'choose
something...', :options_list => [[:one, 'One'], [:two, 'Two'],
[:three, 'Three']], :selected_value => :two

I didn't actually test this, so you may have to tweak it to get it
working. However this is basically how I would solve this problem.
Reply all
Reply to author
Forward
0 new messages