Rails ActionMailer view text indentation problem - ruby-on-rails

When i indent code in a mailer-view, i see the indentation in the sent (plain text-)mail, too. Is there a way to avoid this without writing my code without indentation…?

I have faced the same issue previously, but at that time I chose not to indent the code.
Perhaps you could make a helper method that removes indentation (assuming that you do not want indentation at all in your mail). Something like:
<% no_indentation do %>
Here goes my content.
<% if #show_extra %>
And this is some extra indented text
<% end %>
<% end %>
And then in a helper:
#some_helper.rb
module MyHelper
def no_indentation(&block)
#Capture the content of the block,
#and replace multiple spaces/tabs with a single space.
end
end
I have not tried this out myself, but it could be worth a try.

Related

Custom Sublime Snippets - HTML Style Formatting

Forgive the vague title, I'm having a hard time figuring out the correct phrase for what I am trying to do.
I have a number of custom snippets I have written to support writing ERB for Ruby on Rails. For this example, I am trying to use an if tag that can be on a single line:
<% if something %>Content Here<% end %>
or multiple lines with indented content:
<% if something %>
Content Here
<% end %>
I am able to get the first (single line) format working fine with the following snippet:
<![CDATA[<% if $1 %>${2:$SELECTION}<% end %>$0]]>
The problem is that if I type in the trigger, hit tab, type in the conditional for $1, hit tab, then hit return, Sublime does exactly what I type and puts the following:
<% if something %>
[cursor]<% end %>
Is there a way in the snippet to instead treat the if and end tags like HTML tags:
<% if something %>
[cursor]
<% end %>
Thanks!
The easiest way is to define a new snippet with a new tabTrigger. It will look something like:
<snippet>
<content><![CDATA[<% if $1 %>
${2:$SELECTION}
<% end %>$0]]>
</content>

Rails HAML helper to display a list item

I am having difficulty getting my helper to display a list item. The markup looks like the following:
- #bars.each do |bar|
<% display_bar(bar) %>
The actual helper looks like the following:
module MyHelper
def display_bar(bar)
type = bar.type
concat(%li.type)
concat(%b some text)
concat(%i some more text)
end
end
What am I doing wrong here?
Such things has to be implemented via partials. Or see 5.
<% won't show you anyting. You're in Haml. It's ERb stuff (but even there it wouldn't have shown anything: you'd forgotten the = sign, it should have been <%=).
About concat(%li.type): you cant put your markup inside your Ruby code. Ruby knows nothing about your %li "code".
Amokrane Chentir already mentioned.
You're trying to reinvent the wheel. Rails already provides magnificent helper for such simple cases.
Take a look:
= content_tag_for(:li, #bars) do |bar|
%b= bar.title
%i= bar.id
UPD: content_tag_for sets styles/ids for each li tag based on the current model instance that makes it easy to implement styling/scripting in the future.
The name of your helper is display_bar not display_event.
You should use = instead of <% %>
- #bars.each do |bar|
= display_event(bar)
EDIT
Oops didn't read carefully the content of display_bar method, as #jdoe mentioned you can't use Haml markup syntax in your Ruby code.

Rails Syntax highlighter albino is not working

I am using syntax highlighter "albino" i my rails project ,but it is not displaying any thing
below i have written the code
in helper model
def highlight(text)
Albino.new(text, :ruby)
end
In the view
<% #codes.each do |code| %>
<%= highlight(code) %>
<% end %>
so can any one help me where i am going wrong
or suggest any good highlighter gem for rails?
Which errors do you get?
You are missing a . after #codes:
<% #codes.each do |code| %>
<%= highlight(code) %>
<% end %>
It looks to me like your helper is creating a new Albino instance but not using actually asking it to syntax highlight.
Change your helper as follows:
def highlight(text)
Albino.colorize(text, :ruby)
end
Have you considered using Google Code Prettify? It's the syntax highlighter used on both Google Code and Stack Overflow, which is likely to mean it's reasonably robust.
I don't normally like putting too much functionality in JavaScript, but it seems to me that syntax highlighting is a reasonable feature to add in this way - after all the code will still be readable without the highlighting.
This is a bit of an old problem, but I just came across it myself.
The problem is that Albino is outputting HTML directly as it's being parsed (I think that's the right word, I'm quite new to this).
For example:
highlight(text)
And text is:
def hello_world
puts "Hello World!".to_s
end
Will result in:
<div class="highlight"><pre><span class="k">def</span> <span class="nf">hello_world</span> <span class="nb">puts</span> <span class="s2">"Hello World!"</span><span class="o">.</span><span class="n">to_s</span> <span class="k">end</span> </pre> </div>
What needs to be done is add .html_safe into your highlight method.
Albino.colorize(text).html_safe
That should work.

How to comment code in Rails views?

As I'm playing with Rails and developing views I often want to comment out code. Simple enough with classes & models but views are a bit more tricky.
What's best way to comment code in a view so it's not interpreted by, well, anything... HTML gives us <!-- commented Rails code here --> though code enclosed here seems to get interpreted anyway?!? Or is there a more Railsy way?
<% code code # comment %> USED to work but I think that was accidental.
You were always supposed to put comments in separate comment tags <%# comment %>
Note NO SPACE before the pound.
Now the old loophole is closed (I forget whether 'now' means Ruby 1.8 or Rails 3 or what) so that:
<% code code # this runs too %>
<% # also runs %>
<%# the only way to comment out %>
I use this all the time
<%# This is a comment %>
The reason Ruby code would be executed inside <!-- --> HTML comments is because all of the server side code (ie. Ruby) is interpreted first, and then the output is sent to the client, at which point the browser interprets <!-- --> as a comment. As the other answers said, use <% #comment %> to comment within a Rails view.
Although (and i'm hoping to be corrected here) you have to be careful because i've had some really strange behavior when doing something like this:
<% if (my_boolean) # Commenting on this if-block %>
where it will affect the HTML that directly follows that (even if it's on another line).
Anyone?
And would this qualify as an answer, or a comment?
<% #comment here %>
:D

Ruby and Rails: Statement Modifiers in Views?

I have this code
<% if approved %>
<td>Flow Number</td>
<% end %>
and I'd like to shorten it using statement modifiers. Of course I can use
<%="<td>Flow Number</td>" if approved -%>
but is there a shorter way? I'd also like to get the markup out of quotes.
You could use "content_tag", which isn't actually shorter, but may be more appealing, keeping HTML out of your ruby blocks:
<%= content_tag :td, "Flow Number" if approved %>
Otherwise, you could consider writing a helper - which may be appealing if you need to reuse similar logic throughout the page (or over several pages).
Maybe HAML?
That'd be:
- if approved?
%td Flow Number
Not exactly what you're after I know.
Yeah, I think a helper method using content_tag internally would be the best short way.
Using a helper method, you could also yield to the desired output like this:
# in view helper
def show_if(condition, wrapper_tag)
condition ? content_tag(wrapper_tag, yield) : ''
end
# in view
<%= show_if(approved, :td) {'Flow Number'} %>
or
# in view helper
def show_if(condition)
condition ? yield : ''
end
# in view
<% show_if(approved) do %>
<td>Flow Number</td>
<% end %>
I like this last method for a nice generic way to show or hide whole blocks based on a condition. Hope that helps!

Resources