3

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


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

Now allowed? yields 0.

It works if I do:


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

What gives?

3
  • what does allowed? do or return?
    – corroded
    Sep 1, 2010 at 15:34
  • allowed? is a helper for a gem I wrote, RESTful_ACL. It deduces what URL you're linking to and displays it based on model-level permissions. It simply yields the link, or nothing at all. RESTful_ACL receives the "0", so its definitely at the HAML/Ruby level.
    – Matt Darby
    Sep 1, 2010 at 19:07
  • I have the same problem. When I pass a block (just a string) using do to my helper method, then yield gives me 1 (Fixnum) instead of the string, and the string is rendered separately before the helper method. However, it works fine with block passed using {}.
    – Andrei
    May 6, 2011 at 15:01

3 Answers 3

1

The cleanest way to do this yield concept to allow whatever content you'd like to be properly captured is:

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

In your case, though, 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 like this:

= allowed_link_to('New', new_video_path)
1
0

Why are you echoing the output of that in the first place? You should be doing:

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

In general, you never want to use the output operator (=) with a block. Stuff outputted in blocks doesn't get returned to the block; it's concat'd directly into the buffer. Using a block like that is likely to produce errors with content out of order.

0
0

This took me a while to find, but this is how you do it:

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

The problem is that haml outputs everything to its own special buffer before sending it on to rack or wherever. So you have to let haml call the block first and buffer it.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.