I'm looking for a shortcut in sublime text 2 that will make a block comment in a .erb file.
For instance say I have this block of code in my .erb
<% #posts.each do |post| %>
<p>my code</p>
<% end %>
And I want to quickly comment out the whole thing like:
<%
=begin %>
<% #posts.each do |post| %>
<p>my code</p>
<% end %>
<%
=end %>
Is there a way to highlight that chunk and hit a keyboard shortcut to add those comment blocks? I've tried highlighting the block then choosing Edit > Comment > Toggle Comment and Edit > Comment > Toggle Block Comment but nothing happens.
I have the ERB Insert and Toggle Commands package installed but maybe I need something else?
The usual Sublime keyboard shortcut to comment/uncomment a selection is cmd-/, and block comment is cmd-opt-/, but these would have the same effect as Edit > Comment > Toggle Comment, so if that isn't working, they probably won't either.
I've tried using these in an .erb template, and not only does it work, it seems to be pretty good about using ruby comments for interpolated segments, and html comments for the rest.
On a Mac I comment out a block of code by selecting the text and hitting command and / at the same time. Another useful one that I recently learned is if you need to indent several lines or take our something from several lines, hold down option and select the lines to you want to adjust.
Related
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>
I have a text_area in my rails app where users can paste plain text or code. I really don't want to ask the user to choose text or code for me but want to make it like a WYSWYG text area
Right now I use pre tag. This renders code comments ok but makes text comments look visually ugly.
I can use a syntax highlighting gem, but this requires me to know that the pasted text is code.
Q: Is there any inbuilt apis in rails/ruby to validate if the value in a text area is code or text?
<% if !comment.content.blank? %>
<p> <pre> <%= simple_format comment.content %></pre> </p>
<% end %>
You can instruct the user to wrap the code section around a specific keyword..for instance {code}
then in your template you can extract and decorate the the code section:
<p> <pre><%= comment.content.scan(/{code}(.*?){code}/m) %></pre></p>
I'm using a js.erb template to render some jQuery. When editing an html.erb file in TextMate, I frequently use the convenient key combo, ctrl+>, to create and then toggle the following tags:
<%= %>
<% %>
<%- -%>
<%# %>
This shortcut doesn't work by default when editing js.erb files. In the Bundle Editor, I found a snippet called "Insert ERb’s <% .. %> or <%= .. %>" under "Ruby". By adding "source.js" to the scope selector I was able to get insertion to work, but when I pressed the key combo multiple times, instead of toggling the tag I got a tag inside of a tag like this:
<%= <%= %> %>
I've tried changing the scope of the command called "Toggle ERb Tags" but I can't seem to get toggling to work. Any suggestions?
Update November 19, 2010:
This is no longer a problem in the new version of Textmate that came out this week: 1.5.10 (1623).
One possible reasono why this is the case is that the snippet that generates the angle brackets for you is defined thus:
<%= $0 %>
This puts this text into your source after the tab-trigger occurs. The $0 is a placeholder for the cursor; it's final resting place after the snippet is completed. Since the cursor rests in the middle and this is a simple snippet, repeatedly performing the tab-trigger will nest these brackets.
To achieve what you want, you have to do it in a script. You can use any scripting language as long as you appropriately specify the shebang line. I am not a proficient scripter so I'll try to solve this using pseudocode.
if selected_text
if no_wrapping_angle_brackets
surround_with_angle_brackets
else
strip_angle_brackets
else
if no_wrapping_angle_brackets
surround_with_angle_brackets
else
strip_angle_brackets
It's not much but I hope this helps
This was fixed with Textmate update 1.5.10 (1623).
I just ran into this problem too, even with updated TextMate and bundles. I fixed it by adding source.js.rails to the scope selector of the snippet "Insert ERb’s <% .. %> or <%= .. %>". Make sure you don't change the scope selector for the similar command "Toggle ERb Tags". This inserts the ERb tags correctly and also toggles them as expected.
Your Ruby on Rails Textmate bundle may be outdated due to changes in Ruby 1.9.
Update your tmbundle and this problem should go away.
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.
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