How to Multiline Ruby Statements

1,090 views
Skip to first unread message

Nicholas Van Weerdenburg

unread,
May 31, 2009, 10:26:40 PM5/31/09
to ha...@googlegroups.com
I want to do the following:
  :ruby
    form_remote_for :portion, Portion.new, :url=> new_portion_url, 
      :loading=> 'Form.disable("portion_form")',
      :complete=> 'Form.enable("portion_form")',
      :html=> {:id=> 'portion_form' } do |f|
    .meal_type{:style=>"float: left;"}

e.g. have my form_remote_for on multiple lines. However, I need to indent the following line (meal_type), and I'm not sure how to do it.

The other option:
    - form_remote_for :portion, Portion.new, :url=> new_portion_url, 
    -  :loading=> 'Form.disable("portion_form")',
    -  :complete=> 'Form.enable("portion_form")',
    -  :html=> {:id=> 'portion_form' } do |f|

Didn't work either due to ";" being added after each line.

Any suggestions?

Thanks,
Nick

Nathan Weizenbaum

unread,
May 31, 2009, 10:31:53 PM5/31/09
to ha...@googlegroups.com
The best way to do this is to take this big glob of code out of your view and move it into a helper. If you don't want to do that, though, use the Haml multiline syntax:


- form_remote_for :portion, Portion.new, :url=> new_portion_url, |
    :loading=> 'Form.disable("portion_form")',
    :complete=> 'Form.enable("portion_form")',
    :html=> {:id=> 'portion_form' } do |f|


Nicholas Van Weerdenburg

unread,
Jun 5, 2009, 4:09:39 PM6/5/09
to ha...@googlegroups.com
Thanks. It didn't occur to me to use that with executable code.

Nicholas Van Weerdenburg

unread,
Jun 5, 2009, 4:26:02 PM6/5/09
to ha...@googlegroups.com
Hmm. Doesn't work for me. Here is the code- a HAMLized example from the Agile Rails book. I've also tried with \ in front of the :'s-

- form_tag('javascript:void(0)') do
  .fake-h1 
    Listing Items
    = text_field_tag 'search', @search
    = observe_field :search, |
      :frequency => 0.5, |
      :update    => 'ajaxWrapper', |
      :before    => "Element.show('spinner')", |
      :complete  => "Element.hide('spinner')", |
      :url       => {:action=>'search', :only_path => false}, |
      :with => "'search=' + encodeURIComponent(value)"
.ajaxWrapper
  = render :partial=>'search'

Any thoughts?

Thanks,
Nick

Nicholas Van Weerdenburg

unread,
Jun 5, 2009, 4:27:24 PM6/5/09
to ha...@googlegroups.com
Forgot the error:

Invalid filter name ":with => "'search=' + encodeuricomponent(value)"".

If I put \: in, I get crazy compile errors...

compile error
/Users/vanweerd/work2/innerplate/app/views/track/_item_search.haml:12: syntax error, unexpected $undefined
...mp =  observe_field :search, \:frequency => 0.5, \:update   ...
                              ^
/Users/vanweerd/work2/innerplate/app/views/track/_item_search.haml:12: syntax error, unexpected ',', expecting kEND
... \:update    => 'ajaxWrapper', \:before    => "Element.show(...

Thanks,
Nick

Nathan Weizenbaum

unread,
Jun 5, 2009, 4:29:01 PM6/5/09
to ha...@googlegroups.com
You have to put the multiline character on the last line as well.

On Fri, Jun 5, 2009 at 1:27 PM, Nicholas Van

Nicholas Van Weerdenburg

unread,
Jun 6, 2009, 12:13:41 AM6/6/09
to ha...@googlegroups.com
Ah! That worked. I'll look at doc more closely next time.

Thanks,
Nick

Chris Eppstein

unread,
Jun 7, 2009, 9:07:48 PM6/7/09
to ha...@googlegroups.com
I have to admit, I find the whole multiline syntax awkward enough that I never use it. I think maybe that was the plan ;-) Helpers FTW.

chris

Abhishek Parolkar

unread,
Jun 7, 2009, 11:09:23 PM6/7/09
to ha...@googlegroups.com
Yes multiline is still remains buggy though , Look at this failing test http://github.com/parolkar/haml/commit/f4b6c189f71fd5f12676ddabbff6ff1bd15a6605

anybody, has solution for this still?

'Abhishek

Nathan Weizenbaum

unread,
Jun 8, 2009, 1:00:58 AM6/8/09
to ha...@googlegroups.com
I'm pretty sure that test passes in the master branch...

Nicholas Van Weerdenburg

unread,
Jun 9, 2009, 8:59:08 AM6/9/09
to ha...@googlegroups.com
I'm  just using it for readability for one long statement that takes a block (usually form_for). Moving this to a helper isn't really much value. Or if I did, I'd prefer after working out my logic.

The | is working well for me now. It's not bad- hardly notice it.

Thanks,
Nick
Reply all
Reply to author
Forward
0 new messages