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' %>
Related
I am pretty new to Rails. One of the requirement is to have a Filter Panel that would appear along each record list and will be used to filter records based on criteria. What actually I am looking for an HTML based UI of it that will contain input fields along with labels that I would like to pass from Controller. Since I will be using it across views so I don't want to put in a view specific folder. What's the best way to accomplish this?
You can actually render partials from any folder. For example, in users/show.html.erb you can render a partial _info from, say, transactions.
<%= render 'transactions/info' %>
A common thing to do is to put such shared partials into a separate directory with a descriptive name (I use "shared").
<%= render 'shared/filter_panel' %>
you can put them in /views/application/ dir, rails automatically look for partials in this directory
if you use the application directory then you can just do render 'partial' from any view and it will render /views/application/_partial.html.erb
you can also create for e.g. /views/admin/base dir (if you have admin/base_controller.rb) and put your admin namespace partials there
if you have many partials, I recommend you put them into subdirectories
I have html content that store in table. In that content I want to pass some ruby code
for example
temp = "<html><head>...</head><body>... <%= #something %> ...</body></html>"
then after I use temp.html_safe or raw temp
but #something is not printing
How can I do this ?
Please Help me
Just doing #temp = "<p>... #{#something} ...<p>" in your code and then in your view <%= #temp.html_safe %> should be enough.
This sort of functionality could get you in some dangerous waters. What if someone compromises your database, edits the code in that record to do something destructive, and then executes that code through your application?
You should look at using Liquid instead of executing ERB directly. (Also take a look at the accompanying Railscast.)
I am new to Ruby on Rails. I know that for each controller you have a specific views folder that holds all of it's views. I also know there is the layout folder for the layouts.
But what if I have a bit of a template that keeps popping up in many templates across the system but it's not a footer or header or otherwise layout related.
I want to refer to it using the <%= render.... %> command but where should I put this template?
Is there a generally agreed upon location?
Can I just create a directory under views and store it there?
Rails will automatically look in 'views/application' and in the folder that contains the current parent view.
That said, you can place partials anywhere you like, and refer to them like so:
<%= render 'foo/bar' %>
As #apneadiving suggests, 'shared' is a good name for the folder.
<%= render 'shared/bar' %>
I'm trying to simplify a view from spree for a tablet, using mobile-fu, I copied the view I want to simplify and named it .tablet.erb and i also had to copy the admin layout and rename it to .tabler.erb, the problem is that the layout uses more partials, and those partials use some more, i'm trying to avoid having to copy all the files just to give them a different name or just plain change the render to tell it to use the html one.
Is there a way to tell it to use html format from that point on? like setting the format recursively?
<% self.formats = [:mobile, :html] %> on each view solved it
Got it from here
I have a directory filled with partials. I'm looking to list ONLY the first h1 tags in each partial. The methods to accomplish this task could probably be modified to grab other elements as well.
Right now I use ruby to open each file, print out the first few characters, close file, and repeat. My ruby file parsing skills are limiting me. Here's the code I have at the moment:
<% Dir["app/views/partials/show/*.html.erb"].each do |f1| %>
<% aFile = File.open(f1, "r") %>
<% if aFile %>
<% content = aFile.sysread(20) %>
<p><%= content %></p>
<% else %>
<%= "Unable to open file!" %>
<% end %>
<% end %>
I also think I'm opening the entire partial in memory? Wondering If I can just read up until I find my h1 tag then close file and move on? Again I'm only reading first 20 characters because I haven't yet grasped a way to search for the first h1 tag.
I'll make edits as I work through the open, parse, piece... I appreciate any guidance and direction you can offer. Thanks!
EDIT:
Based on comments below there may be a far better way to accomplish my task. So I'm providing some additional background to get direction on other solutions.
This is for a slide show based on partials in a directory. The slide show is controlled with a navigation element which I would like to populate by the h1 tags in the partials. I'm not going to manually enter these things every time a change is made! I want the end user to simply drag and drop partials into a directory (with a certain name convention and h1 tag description for navigation) and let the slide show do everything else.
I could impose a class on the h1 tag "forNavigation" and on the content "sliderContent" and then use jquery to create a post load <ul> but that doesn't seem right. Plus they'll all be part of the same rendered div.
I guess I'm not clear why reading the first 50 characters of a partial, copying whats in the h1 tags, and putting it in a isn't the most elegant solution?
Like I said, above does everything needed except copy and print whats between the first h1 tag... With an xml parser or some regexp it'll be done. I'm just no good with parsing files.
Please let me know other methods to approach this. Right now I still think it's best to parse the partial (with or without rendering) and put what I need where I want it as needed.
Partials are not meant to be "parsed", but to be rendered inside other partials and templates. If you need to grab a part of a partial, you should probably extrat that part as a further partial, and use that inner partial in both the "listed" partial and in the "aggregated" view.