如何在 rails 中使用 Arbre gem?

How to use Arbre gem in rails?

我想尝试 rails 的 Arbre gem。此页面上有一个示例:https://github.com/activeadmin/arbre/blob/master/README.md

我必须在哪里粘贴下一个代码?

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

我想试试上面的代码,但我不知道必须将它粘贴到哪里。哪个文件?哪种方法?我将代码粘贴到我的控制器中,但它不起作用。

在您粘贴的示例中,html 变量现在包含您页面的 html。

您可以像这样在控制器中渲染它:

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

此外,Github 页面上没有详细记录,但通过挖掘源代码,您似乎也可以使用 abre 模板代替常规的 erb 视图(注意.arb 文件扩展名):

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

那样的话,您的控制器可能看起来像这样:

app/controllers/whatever_controller.rb

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