开发者

How to render partial in HAML that only contains a conditional block

开发者 https://www.devze.com 2023-03-15 07:18 出处:网络
I\'m new to Ruby and HAML and attempting to render the following partial inside of main HAML file. The partial consists of only an if conditional. I would like the if conditional to return the output

I'm new to Ruby and HAML and attempting to render the following partial inside of main HAML file. The partial consists of only an if conditional. I would like the if conditional to return the output of the partial's HAML code to the main template if the condition is met, and to render nothing if it isn't. The following code works if the array "attachments.each_file" is empty (it renders nothing as I would like), but if it isn't empty it throws an error when it tries to proceed into the if conditional's code. Here are the relevant code snippets:

Error Message:

LocalJumpError in Questions#show

Showing /questions/_attachments.html.haml where line #1 raised:

no block given (yield)

Main HAML template code:

= render "slashbias/questions/attachments", :attachments => @question.attachments, :editing => false

Partial HAML code:

- if !attachments.each_file.empty? 
  %dl#attachments_list
    %dt.header Attached files:
    %dd
      -attachments.each_file do |key, file|
        = link_to file.name, question_attachment_path(question.group, question, file, key)
        -if editing
          = link_to t("scaffold.destroy"), remove_attachment_question_path(question, :attach_id => key), :class => "r开发者_运维问答emove_attachment_link"


I believe attachments.each_file on line 1 is expecting a block. It doesn't throw an error in the case of having 0 files, because it never attempts to yield anything. But in the case that there are files, each_file is trying to yield the files to a block and raising the error you're seeing because there is no block to yield to.

Is there another way for you to test if there are any files? Something like !attachments.files.empty? or attachments.files.count > 0?

0

精彩评论

暂无评论...
验证码 换一张
取 消