简体   繁体   English

Rails-从Yaml文件加载内容的文件路径

[英]Rails - File path to load content from a yaml file

I'm have a variety of text files with static long form text as content. 我有各种各样的文本文件,其中包含静态长格式文本作为内容。 Right now I am storing them in a separate "content" file in the config folder. 现在,我将它们存储在config文件夹中单独的“内容”文件中。 For instance "../config/content/content1.yml" "../config/content/content2.yml" and so on. 例如“ ../config/content/content1.yml”、“../config/content/content2.yml”等等。

I would like to string these files together in my application. 我想在我的应用程序中将这些文件串在一起。 So in my controller I have variables that attempt to pull the content of each file, for example 因此,在我的控制器中,我有一些变量会尝试提取每个文件的内容,例如

@content1 = YAML.load_file("#{Rails.root}/app/config/content/content1.yml")
@content2 = YAML.load_file("#{Rails.root}/app/config/content/content2.yml")

I then try load that variable into my view with 然后,我尝试使用

<%= @content1 %>
<%= @content2 %>

This and everything else I've tried doesn't seem to work though. 尽管这一切以及我尝试过的其他一切似乎都没有效果。 I'd really just like to get the text to display in my view. 我真的很想让文本显示在我的视图中。 Any help to point me in the right direction would be much appreciated. 任何帮助我指出正确方向的帮助将不胜感激。 I'm very noobish with rails still. 我仍然很讨厌带铁轨。

I don't know why you don't want to store this data in DB, but lets describe what you need using only views. 我不知道为什么您不希望将这些数据存储在DB中,而仅使用视图来描述您所需要的内容。


According to comments that what you need are partials. 根据评论,您需要的是局部的。

Example: 例:

Lets have file app/views/content/editor1/paragraph1.html : 让我们拥有文件app/views/content/editor1/paragraph1.html

 Example paragraph

Then in your view you can render this using: 然后,在您的视图中,可以使用以下方法渲染该图像:

<%= render partial: 'content/editor1/paragraph1' %>

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

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