Does the HAML block return "0" on output?

I just upgraded to Rails3, Ruby 1.9.2 and the latest HAML gem. This code was used to work:


  = allowed? do
    = link_to('New', new_video_path)

Now allowed?gives 0.

It works if I do:


  = allowed?{ link_to('New', new_video_path) }

What gives?

+3
source share
3 answers

The cleanest way to do this is to provide a concept that allows any content that you want to capture correctly:

= allowed? do
  - capture_haml do
    = link_to('New', new_video_path)

In your case, however, why not just write another helper method?

def allowed_link_to(*args, &block)
  opts = args.extract_options!
  if allowed? args.last
    link_to args.push(opts), &block
  else
    ''
  end
end

And use it as follows:

= allowed_link_to('New', new_video_path)
+1
source

Why do you echo the result of this in the first place? You must do:

- allowed? do
  = link_to('New', new_video_path)

, (=) . , , ; . , , .

0

, , :

def wrap_in_div(&block)
  "<div>#{capture_haml(&block)}</div>"
end

The problem is that haml dumps everything into its own special buffer before sending it to the rack or anywhere. Thus, you must allow the hamlet to first call the block and buffer it.

0
source

Source: https://habr.com/ru/post/1762686/


All Articles