-3

我想试试 Arbre gem for rails。此页面上有一个示例: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

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

4

1 回答 1

3

在您粘贴的示例中,该html变量现在保存您页面的 html。

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

应用程序/控制器/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

这样,您的控制器可能如下所示:

应用程序/控制器/whatever_controller.rb

def show
  # nothing necessary here, by default renders show.html.arb
end
于 2015-07-31T14:16:48.020 回答