0

So I've written this code for erb awhile ago

<% if @posting.pictures.blank? %>
<% else %>
    <script>
        $("#slider-posting").vegas({
            slides: [
                    <%- @posting.pictures.each do |p| %>
                        {src: '<%=p.url%>'},
                    <%- end %>
            ],
            transition: 'slideLeft'
        });
    </script>
<% end %>

I'm having a bit of trouble converting it to HAML.

Trying conversion based on http://html2haml.herokuapp.com/

- if @posting.pictures.blank?
- else
  :javascript
    $("#slider-posting").vegas({
                slides: [
    <haml_silent>                    @posting.pictures.each do |p| 
    </haml_silent><haml_block>                      {src: '#{p.url}'},
    </haml_block>           ],
                transition: 'slideLeft'
            });

Causes the following error

undefined method `url' for nil:NilClass

on this line

</haml_silent><haml_block>                        {src: '#{p.url}'},
Kishe
  • 9
  • 3

2 Answers2

1

try this

- if @posting.pictures.blank?
- else
  :javascript
    $("#slider-posting").vegas({
      slides: [
        #{
          @posting.pictures.map { |p| "{src: '#{p.url}'}"}.join(',')
        }
      ],
      transition: 'slideLeft'
    });
Long Nguyen
  • 373
  • 2
  • 9
0

Have you tried omitting ? after if statement, it's normally used when you're using ternary operator in your code, so please try omitting that part and try again.