CSV header styling in RoR - ruby-on-rails

I'm using this to export records to CSV format. But i want to give styles to headers.
Here is my .erb
<%= CSV.generate_line(["X"+"\t"+"Y"+"\t"+"Y"+"\t"+"T"]).html_safe %>
<%#coupons.each do |coupon|%>
<%= CSV.generate_line([coupon.x+"\t"+coupon.y+"\t"+coupon.z+"\t"+coupon.t]).html_safe %>
<%end%>
How can i give styles to headers or columns? At least i want to make them bold:)
Thnaks.

CSV is a plain text format so you cannot add styles to it.

To use styles, formatting, charts etc you will need to generate an ecma-376 (eg excel) or odf file. If you are on RoR, you might have a look at this:
http://rubygems.org/gems/axlsx

Related

Rails 4: how to insert line breaks in text_area?

I have created a blog in rails. I'm a beginner and got quite far, but now I'm stuck with a seemingly minor detail: I can't seem to format the posts (articles).
Here's the relevant part of my show.html.erb:
<p>
<strong>Content:</strong>
<%= simple_format (#article.content) %>
</p>
When I write something and insert html-tags, they are not recognized as such. What am I doing wrong?
Rails will automatically remove html tags to prevent someone from injecting code into your webpage (e.g. malicious javascript)
If your users cannot enter data into #article.content and it's always safe then you can flag it as safe usng the html_safe method.
<%= (simple_format (#article.content)).html_safe %>
Can you post the article content for reference? If I had to guess, I'd imagine Rails is escaping the html tags and inserting them as plain text (so the output looks like: Article content !
Take a look at Rails' helper methods like content_tag (http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag) and concat (http://apidock.com/rails/ActionView/Helpers/TextHelper/concat) and consider using those to help with generating the appropriate html tags.
An issue to be concerned with is who's going to be supplying the content. For example, if you're writing an application that other people will use, you want to make sure any html give you is escaped to avoid XSS attacks. In that case, you'll want to spend some time reading about how to properly sanitize user input.
You can now specify the tag it gets wrapped in (defaults to p) like so:
<%= simple_format (#article.content, {}, wrapper_tag: "div") %>
or
add white-space: pre-line style.
It will display \r or \n (enter) in user input as a new line.
for more info:
http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format

Can axlsx convert html format to work in excel?

My DB stores text from a WYSIWYG editor that looks something like this:
<p><s>Hi!</s></p>
<p>My Name is Bob's.</p>
<p> </p>
<p>I like to eat these things:</p>
<ul>
<li>Candy</li>
<li>Veggies</li>
<li>Everything</li>
</ul>
<p>Enjoy<sup>2</sup></p>
In my view I have something like:
sheet.add_row [#event.text], style: font_format
where #event.text is the above html
Is there a way to make this formatting work in excel using axlsx?
I don't think there is any automatic conversion of html to styles. You'd have to write one yourself. I would use the rich text example as a guide.
I believe it handles any normal Axlsx style on a chunk of text. It at least handles bold, italic, and strikethrough.
For a forced line feed use "\x0A" (breaks between paragraphs.)
But, that means you'll have to parse the html.
Yes, to_spreadsheet is just for you. I have just finish a Rails app using it to generate xlsx file for download. I just follow the instruction and create a view 'show.xlsx.erb' in view directory. And it's done!

Can we create an Image by HTML string in Ruby On Rails.

I would like to know , Is there any possibility by which we can create an image by the HTML sting that has the HTML tags along with the formatting Or the HTML content coming from the Web Editors like Ckeditor or TinyMce etc in Ruby on Rails.
Thanks
Nishant
Are you referring to achieving:
<%= "<img src='http://domain.tld/some_image.png' />".html_safe %>
You can also interpolate any strings by doing
<%= "#{url_string}".html_safe %>
where url_string = "<img src='http://domain.tld/some_image.png' width='200px'/>"
I guess you want something like following
<% str = "<img src='/images/InboundButtons.png' style='border: medium none ; padding: 0px 0px 0px -2px;'>"%>
<%= str %>
Where str is an html string and you can show it as above.
It seems that your ultimate destination is a PDF file. I would skip image creation, and go right to that, using one of several HTML to PDF libraries available, perhaps one of the ones listed right here on StackOverflow. If you do need an image for some reason, even then, I would consider generating a PDF of your HTML, and then converting that to an image, since it's the best path I know to get from HTML to an image.
However, you have done a poor job of describing your problem, so with more details there might be some obvious alternative as well.

Adding Markdown into my Rails 3 app

I am trying to add Markdown to my Rails 3 web app but am having problems.
I have tried rdiscount and markdownizer but either they're not working or I'm not writing the correct code for them.
The code I have at the moment to display a text field is <%=h simple_format (#user.desktopinfo) %>
I want to increase the functionality of this text by adding Markdown but I am unable to get it work, please help! :)
EDIT 2
Using markdownizer broke my app, so I am now using BlueCloth. Add bluecloth to the gem file and add this <%= raw BlueCloth.new(#user.desktopinfo).to_html %>
:)
EDIT
Actually, just trying again...
With markdownizer, with markdownize! :desktopinfo in the user model and <%= #user.rendered_desktopinfo %> on the page that shows the text, I get this: <h1>this is a h1</h1> on the text when I enter
this is a h1
============
so I am halfway there! How do I now turn this code into html?
Consider rdiscount which substitutes for bluecloth but is faster and better maintained.
Ryan Tomayko's comparison is a good write up regarding the different libraries for using markdown in Ruby.
You haven't really specified exactly what you are after, but I use bluecloth when working with markdown. You can add 'bluecloth' to your Gemfile.
To parse your markdown it is as simple as:
<%= raw BlueCloth.new(YOUR_MARKDOWN).to_html %>
You need the keyword raw. so the HTML is not escaped.
<%= raw #user.rendered_desktopinfo %>

simple_format and embed's

I've tried searching the web for a solution for this but havent been able to find any way of combining this.
Im printing html from a wysiwyg editor and found the simple_format helper to print the HTML with paragraphs etc. The problem is that it strips embed tags from the code aswell.
Do any of you know a way to print content from a WYSIWYG that adds paragraphs, br's, strong/bold and keeps all media such as images, embed's, etc.
Thanks!
Incase anyone is interested i solved it with:
<%= simple_format(content.post_content, {}, {:sanitize => false}) %>

Resources