简体   繁体   中英

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. For instance "../config/content/content1.yml" "../config/content/content2.yml" and so on.

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.


According to comments that what you need are partials.

Example:

Lets have file app/views/content/editor1/paragraph1.html :

 Example paragraph

Then in your view you can render this using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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