user3062123
user3062123

Reputation: 237

Ruby / Slim concatenation

I have a loop that checks the index against each value to build a breadcrumb, this data is passed in to a render using each slug as a separate hash in an array. Currently i'm getting a lot of "+" is not a method errors, but i have no idea how said line is meant to look in slim. The purpose of the line is to loop through the breadcrumbs array, adding the slug of the breadcrumb onto the previous one repeatedly, along with a slash before to create a usable url. Does anyone know the correct way to display this?

Code:

.c-fg
  ol.crumb
    - properties[:breadcrumbs].each_with_index do |breadcrumb, index|
      - url += "/" + breadcrumb[:slug]
      li
        a[href="#{url}" title="#"]
          = breadcrumb[:place]

Error Message:

undefined method `+' for nil:NilClass

Upvotes: 0

Views: 2467

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

.c-fg
  ol.crumb
    - properties[:breadcrumbs].each_with_index do |breadcrumb, index|
      - (url ||= '') << "/" << breadcrumb[:slug]
      li
        a[href="#{url}" title="#"]
          = breadcrumb[:place]

It’s always better to update the string inplace with String#<< rather than produce an amount of temporary intermediate string objects with String#+.

Upvotes: 4

Related Questions