0

Sorry if this is a stupid question, but I would like to know how one can go about defining a class in an Arbre block. I tried to simply put the comma after the block, but Ruby accuses it of an error (specifically: "unexpected token tCOMMA (Using Ruby 2.3 parser; configure using TargetRubyVersion parameter, under AllCops) (error:Lint/Syntax)").

This is how I tried to do it:

div {
    h4 "Title", class: "title"
    span "Info", class: "info"
}, class: "my_div"

this results in the aformentioned error. But I don't really know any other way to go about this. Any help would be much appreciated!

Progman
  • 16,827
  • 6
  • 33
  • 48
Victor BC
  • 103
  • 4

1 Answers1

1

Arbre definitions are ruby methods, that has a basic syntax that you have to follow. First comes the positional arguments, then keyword arguments and the last a block. In your example you do it in the other way around, so you pass a block then a keyword argument. To fix this simply change to:

div class: "my_div" {
    h4 "Title", class: "title"
    span "Info", class: "info"
}

Which is equivalent to:

div(class: "my_div") {
    h4("Title", class: "title")
    span("Info", class: "info")
}
David
  • 370
  • 3
  • 6
  • Thank you very much!! – Victor BC Feb 24 '21 at 19:21
  • I tested it and unfortunately it creates a problem. Now it does not recognize a variable I have from outside of it (I use this variable to get the data I want to display). But everything else works, so your answer is basically correct! So thank you – Victor BC Feb 24 '21 at 19:28
  • In the [documentation](https://activeadmin.github.io/arbre/) they mention this [context](https://www.rubydoc.info/gems/arbre/Arbre/Context) that can be used to pass locals to components. Also there is an option to pass instance variables in the controller, if that fits better your use-case. – David Feb 24 '21 at 19:40
  • There was no need, I found out that the second one you suggested worked! – Victor BC Feb 24 '21 at 19:51