ruby-on-railsarbre

How to use Arbre gem in rails?


I want to try the Arbre gem for rails. There is an example on this page: https://github.com/activeadmin/arbre/blob/master/README.md

Where must I paste the next code?

html = Arbre::Context.new do
  h2 "Why is Arbre awesome?"

  ul do
    li "The DOM is implemented in ruby"
    li "You can create object oriented views"
    li "Templates suck"
  end
end

I want to try the code above, but I don't know where I must paste it. Which file? Which method? I pasted the code into my controller, but it doesn't work.


Solution

  • In the example you pasted, the html variable now holds the html for your page.

    You can render it in the controller like this:

    app/controllers/whatever_controller.rb

    def show
      html = Arbre::Context.new do
        h2 "Why is Arbre awesome?"
    
        ul do
          li "The DOM is implemented in ruby"
          li "You can create object oriented views"
          li "Templates suck"
        end
      end
      render html: html.to_s
    end
    

    Also, it isn't well documented on the Github page, but digging through the source, it appears you can also use abre templates in place of regular erb views like so (notice the .arb file extension):

    app/views/whatever/show.html.arb

    h2 "Why is Arbre awesome?"
    
    ul do
      li "The DOM is implemented in ruby"
      li "You can create object oriented views"
      li "Templates suck"
    end
    

    That way, your controller could look like this:

    app/controllers/whatever_controller.rb

    def show
      # nothing necessary here, by default renders show.html.arb
    end