heroku re-orders ruby on rails loop output of partials - ruby

I have a page which renders several partials; on my local mac everything is fine but after I push to heroku and visit the page the order of the partials is different!
I render partials with the code below and thought I controlled the order with a file naming convention.
html.erb:
<% Dir["app/views/partials/ws/*.html.erb"].each do |ws| %>
<%= render 'partials/ws/' + File.basename(ws,'.html.erb').slice(1..-1) %>
<% end %>
The partials use a naming convention:
_ws_01-why.html.erb
_ws_02-what.html.erb
_ws_03-who.html.erb
_ws_04-where.html.erb
_ws_05-when1.html.erb
_ws_06-how.html.erb
Heroku renders in this order:
_ws_01-why.html.erb
_ws_02-what.html.erb
_ws_06-how.html.erb
_ws_04-where.html.erb
_ws_05-when1.html.erb
_ws_03-who.html.erb
I'm not sure how heroku is interpreting the naming convention / ruby loop order... Wondering if there is a better naming convection or logic to add in my loop to control the order?
Thanks!

Change this:
<% Dir["app/views/partials/ws/*.html.erb"].each do |ws| %>
to this:
<% Dir["app/views/partials/ws/*.html.erb"].sort.each do |ws| %>
You cannot be sure than an enumerator will choose the same order in all cases unless you force it somehow.

Related

Unable to use findAll() to search sub-directories

I'm using an eco renderer within Docpad (building a site based on https://github.com/zenorocha/browser-diet)
In it, I need to find all the documents in a particular sub-directory.
The directory structure is:
/src/documents/test1/test2
I need to find the documents stored in test2
I can find all the documents in test1 using:
<% for item in #getCollection("documents").findAll({ url: $startsWith: '/test1'}, [order:-1]).toJSON(): %>
No documents are returned when I add an extra sub-directory, using:
<% for item in #getCollection("documents").findAll({ url: $startsWith: '/test1/test2'}, [order:-1]).toJSON(): %>
There are documents at that URL, so what am I doing wrong?
I think you are doing it right. I just tested it in my project with the following code (tools is my generic collection defined in docpad.coffee):
<% for docu in #getCollection("tools").findAll(url:$startsWith:"/tools/adminui").toJSON(): %>
<% console.log(docu.url) %>
<% end %>
and in the console I get only the list of urls that start with /tools/adminui.
Just try to investigate it further with console output or share more details

Dynamically Writing in generator template SCSS file

I'm trying to create my own generator in rails for my standard set up of assets for the pipeline. I've been following Ryan Bates Railscast on the generators
He explains how to add dynamic code the html.erb files but I tried to do something similar in scss file and it simply outputs the tags:
<% if options.add %>
//Some optional code here
#import "add.css.scss";
<% end %>
But instead of executing it when I call create_template it just adds the code into the template. I've tried calling the file .scss.erb but no joy with that either?
Any ideas on how I might tweak the files to make the if statement work?
Try this <%- if options.add -%>

Ruby rails - list active sessions

Is there any way to get the list of all active sessions in rails? I have memory cached session list, and I want to view the user ids, source ip, time stamp, duration etc. for basic administration purposes.
To expand on my comment, I am doing something similar to what you want to do. I am using authlogic but I imagine that you can do something equivalent with other authentication packages.
I am using authlogic's auto logout capabilities and have it set to log people out after 120 minutes. Knowing that, generating a list is simple. I just do the following:
<% User.find(:all, :conditions => ["last_request_at > ?", 120.minutes.ago]).each do |user| %>
<li>
<%= link_to user.name, admin_person_path(user.bio) %>
(<%= mail_to user.email %>) <%= time_ago_in_words(user.last_request_at) %> ago
</li>
<% end %>
You can see that I just list their names, e-mails, and when they last made a request. It would be trivial to add last_login_ip, current_login_at, etc.
Hope this helps.

Rails3 and automatic nofollow links

i have some model, let it be Post with field :content. Any user can submit post with html (with links of course:) ) and i'd like to set nofollow on those links. Is there any rails plugin to automate this task? Does this plugin have ability to manage "nofollowing" in conditional way - e.g. admin can add links without nofollow, but other - with only nofollow?
This should do what you're looking for: https://github.com/rgrove/sanitize/
Install the plugin, then for a block of text you can run:
<%=raw Sanitize.clean(#your_html, Sanitize::Config::BASIC) %>
There are other options that you can use to customise it, but the Config::BASIC version will detect all links in that block of text and add the nofollow tag to them.
You can define a helper for this, overriding (or rather wrapping) the default link_to
In app/helpers/posts_helper.rb do something along the lines of:
def nf_link_to(link_text, post)
opts = {}
opts[:rel] = "nofollow" unless post.author == "admin"
return link_to text, post, opts
end
So that in your view you can do:
<%= nf_link_to post.title, post %>
Which should result in:
My First[tm] Post
You should have a good look at the actual implementation of link_to and make your ''nf_link_to'' as complex as (as in; passing arguments and perhaps a block) as you desire.

Erb with Sinatra in ruby

So I have a webserver I've built using sinatra, the meat of which goes like this:
set :variable,"value"
get '/' do
erb :index
end
And, of course, the template in views/index.erb which looks something like this:
<html>
<!-- etc -->
<ul>
<% my_array.each do |thing| %>
<%="Something: #{thing}, variable from sinatra: #{settings.variable}"%>
<% end %>
</ul>
</html>
If you try running code like this you'll notice that you can't access sinatra's settings variable from inside erb templates. Any ideas how I can achieve this while keeping its simplicity?
Thanks in advance!
I was using an old version of Sinatra - updated to version 1.0 and it works fine :)
Thanks everyone!
I had a similar issue and the resolution was to make sure all the set :x, "y" stuff happened in the class declaration (of my subclass of Sinatra::Base) outside of the initialize method.

Resources