简体   繁体   English

如何在导轨中使用 Arbre gem?

[英]How to use Arbre gem in rails?

I want to try the Arbre gem for rails.我想尝试使用 Arbre gem for rails。 There is an example on this page: https://github.com/activeadmin/arbre/blob/master/README.md此页面上有一个示例: 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.我将代码粘贴到我的控制器中,但它不起作用。

In the example you pasted, the html variable now holds the html for your page.在您粘贴的示例中, html变量现在保存页面的 html。

You can render it in the controller like this:您可以像这样在控制器中渲染它:

app/controllers/whatever_controller.rb应用程序/控制器/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):此外,它在 Github 页面上没有得到很好的记录,但是通过挖掘源代码,您似乎还可以使用abre模板代替常规的 erb 视图,如下所示(注意.arb文件扩展名):

app/views/whatever/show.html.arb应用程序/视图/无论/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应用程序/控制器/whatever_controller.rb

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM