Im currently working with the mirror quick start demo from Google Glass, which is built in Ruby using Sinatra to make the web app. Also it uses haml to render the website. My question is:
I can get the date from the timeline card using
Rack::Utils.escape_html(timeline_item.created)
But it give me back the UTC time. I already tried using strftime but its not working (it says it's a String).
Yeah I'm new on this, Any help? please??
heres the code of the layout file on haml
.span4
%table.table.table-bordered
%tbody
%tr
%th Attachments
%td
- if timeline_item.attachments.length > 0
- timeline_item.attachments.each do |attachment|
%img{src: "/attachment-proxy?timeline_item_id=#{timeline_item.id}&attachment_id=#{attachment.id}"}
- else
.muted None
%tr
%th Text
%td= Rack::Utils.escape_html(timeline_item.text)
%tr
%th HTML
%td= Rack::Utils.escape_html(timeline_item.created)
%tr
%td(colspan="2")
%form.form-inline(method="post" action="/delete-item")
%input(type="hidden" name="id" value="#{timeline_item.id}")
%button.btn.btn-block.btn-danger(type="submit") Delete
i suspect this might help
%td= timeline_item.created.strftime("%B %d, %Y")
meaning, don't use the escape_html thing on your time objects, that will convert it into a String and you'll get the crash you had
Related
I'm working on a project using Middleman. In one of the pages (videos.html.markdown.erb), I'd like to add partials working with both markdown and Middleman helpers.
<h3><%= video.title %> : RĂ©cit de tournage</h3>
<%= partial "partials/shootandlook1" %>
</div>
It works fine except that Markdown is not converting into HTML... :-(
I named my partial _shootandlook1.html.markdown.erb and my page videos.html.markdown.erb.
I really don't understand what I did wrong... Could someone please help me?
The whole source code is here.
Many, many thanks in advance!
This should work fine if you name your page template file videos.html.erb, and name your content partial _shootandlook1.md.
The Markdown file will be processed first, then inserted into the ERB template appropriately.
I usually find that it's best to avoid having multiple template formats in one file, unless the format explicitly supports blocks (like Haml)
I have the following code in a form partial:
<%= f.association :client %>
<%= f.input :url, :wrapper => :prepend do %>
<%= content_tag :span, "http://www.", :class => "add-on" %>
<%= f.input_field :url, :class => 'span4' %>
<% end %>
Trying to load anything to do with this form partial e.g. new/edit etc causes Heroku to error. It works flawlessly on localhost.
The only obvious error in the Heroku logs is:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
I'm afraid I don't really understand that but thought it might help anyone trying to answer this.
If I change the form partial code it works on Heroku no problem at all e.g.
<%= f.input :url, :input_html => {:class => 'span4' } %>
Obviously I lose my nice bootstrap add-on span so I'd rather find out what the issue is rather than running around it.
Shot in the dark, but maybe an initializer that sets up the prepend wrapper (like initializers/simple_form.rb or initializers/simple_form_bootstrap.rb) is not being run by Heroku, either because it's not checked into git, or because of some environment-specific logic in the initializers or environment files.
Well after testing and some help from Dan I finally got to the bottom of it. Unfortunately, it seems to be quite localised to my situation though.
But just in case anyone stumbles across this, the error I had was the following:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
That was in the log and was THE SAME when I followed Dan's advise to turn on error reporting on the production site (set consider_all_requests_local = true in config/environments/production.rb).
I thought that the error was being caused in the template, however, it was some validation rules in the model. The validation rules for me where some regex to stop the user starting the input with a certain string. They worked perfectly fine on my local, but Heroku really wasn't liking it!
I removed those validation rules and voila, my nice bootstrap add-on span was working perfectly.
So in my DB I want stored a ruby/HTML statement in the Database Table such as -
p This site is owned from 2000 - #{Time.now.year} by Acme Widget Co. /p
Or let's say that I want people to be able to include in other code snippits - e.g.
A client wants to run in a show view in the middle of a paragraph some magical partial such as #{render 'my_magical_code'}
On the view I have this being rendered as <%= raw(#page.content) %>
But its like a double rendering and rails will just put on the page #{render 'my_magical_code'} or #{Time.now.year}
So can / how do I solve this?
Thanks!
There are work arounds but I think the most proper way to solve your problem is to store your information in a better way. Add a migration to your model with a year_founded and company rows. You can then call these in your views rather than storing the HTML. Eg. This site is owned from <%= #object.year_founded %> to <%= Time.now.year %> by <%= #object.company %>
How to create automatic hyperlinks for urls in the text/string rendered on the view?
I have a page that renders user activity log and in that log/text/string there are some random urls which I want to automatically hyperlink to open in a new browser window. There is this auto_link in ruby rails, how do I use that?
text = "User xyz accessed url - http://www.something.com on 04/13/2012 00:13:18GMT"
<%= "#{Text}" %>
I want this rendered with a hyperlink to the url. The URL could be anything anywhere in the text.
Use auto_link like this:
<%= auto_link(text) %>
If you want the generated links to open new browser windows (or tabs) add the option like so:
<%= auto_link(text, :html => { :target => '_blank' }) %>
As mentioned by pjumble in the comments, auto_link is no longer a part of Rails core as of Rails 3.1, this gem brings it back: https://github.com/tenderlove/rails_autolink Thanks pjumble!
For faster parsing, try https://github.com/vmg/rinku
require 'rinku'
Rinku.auto_link(text, mode=:all, link_attr=nil, skip_tags=nil)
Also one often need more filters than just linking, for example Youtube embedded videos. For this use: https://github.com/dejan/auto_html
Rails 3.1 and Devise 1.5 question.
I'm using the following code to display flash messages in my layout:
<% flash.each do |key, message| %>
<%= content_tag(:div, message, :class => "flash #{key}") %>
<% end %>
I'd like to change the css class for some of my confirmation messages from notice to success, but I don't know where to override or change the key because I don't know where it's set.
Can anybody point me in the right direction?
Thanks!
So the way to do this is to edit the devise controllers.
When you install Devise through the normal installation I don't think it install the controllers (at least it didn't for me).
So first you should manually add the controller files and put them into your project in the same location: https://github.com/plataformatec/devise/tree/master/app/controllers/devise
Once you have the files in your project, go to the "sessions_controller.rb" file.
One lines 16 & 25, you should see the code:
set_flash_message :notice, :signed_in
and
set_flash_message :notice, :signed_out
You just need to change ":notice" to ":success"
Hope that works!
I ran into this problem because I'm using the bootstrap-sass gem which uses a whole different class structure for flash messages. I took the route of solving this with sass rather than messing with devise at all (sass inheritance is awesome:).
So, I render the flash messages in app/view/layouts/application.html.haml:
%div.container
- flash.each do |key, value|
= content_tag(:div, value, class: "alert alert-dismisable alert-#{key}")
And, I add a simple rule to app/assets/stylesheets/custom.css.scss to get Devise's flash messages working:
/* Style Devise Flash messages like Bootstrap */
.alert-alert {
#extend .alert-warning;
}
That's it! now flash[:alert] is styled just like bootstrap's flash[:warning].
The css class is being set by the :class argument. To add a class of notice (or success, etc.), just change your call to the following:
<%= content_tag(:div, message, :class => "flash #{key} notice") %>