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.
Related
Rails 7 / Ruby 3
I'm currently working on a site that requires code examples to be displayed on a page - I can get these to display utilising the extra % character trick, however, for some of the examples I need to have a variable within them that is resolved (e.g. like the users' own API key etc...).
Consider I have #variable = "Resolved Variable"
<%%= link_to #variable, variable_path %>
Outputs on the page explicitly as
<%= link_to #variable, variable_path %>
But I really need the #variable to resolve and show on the page as:
<%= link_to "Resolved Variable", variable_path %>
I've tried all kinds of escaping the variable, but it seems that <%%= ensures that nothing following it can be resolved.
Any ideas?
Any text you haven't html_encodeed will be displayed as plain text.
My suggestion to you is to create a interpolated string that you could use to generate your intended result. For example:
output_text = "<%= link_to '#{#variable}', variable_path %>"
And, not sure if this is what you're looking for, but you can get a good UI by adding some Javascript library to format you code in the language in intend (in this case Ruby, it seems).
In case that's interesting to you, check the Prism lib, or check how to add it to your project here
I hope this helps.
With kind regards,
Rogerio
<%% in ERB will simply output <%, no more, no less. In particular, it won't attempt to parse the code after <%% as Ruby. However, this doesn't mean that you can't have another <%= ... %> after <%%:
require 'erb'
template = <<-EOD
<%%= link_to <%= #variable.inspect %>, variable_path %>
EOD
#variable = "Resolved Variable"
puts ERB.new(template).result
The inspect method will add quotes around your string and also escape certain characters as needed.
Output:
<%= link_to "Resolved Variable", variable_path %>
I'm working on a web application that has a view where data is fetched and parsed from a text file (the textfile is only available at the backend, not to the user). I've written a function that takes in the text file and converts it to an array of strings, it's called txt_to_arr. Then I have another function line_fetcher which just calls txt_to_arr and outputs a random string from the array.
In my view, I call the controller's function as so: <% line_fetcher %>.
I've put both txt_to_arr and line_fetcher into the view controller's helper rb file, and when I run rails s, the random string is not rendered at all. I've also tried <% puts line_fetcher %>
I've checked in Bash that the function does output random strings from the text file, so the function does work correctly. Also, the text file being parsed is in the public folder. Does anyone have an idea why this might be?
Thanks a lot!
Try placing the code in the controller and assigning the output to a variable using
a=`line_fetcher` (note the backtics) as detailed at
http://rubyquicktips.com/post/5862861056/execute-shell-commands
and then <%= a %> in your view.
and place the file in the root of your rails app
Simple erb like <%= line_fetcher %> would work good for simple variables.
But if you want output of any model/database instance then do:
<%= ModelName.first.inspect %>
Note the inspect word.
And in case of using HAML do:
=ModelName.first.inspect
In ERB: The <% %> signify that there is Ruby code here to be interpreted. The <%= %> says interpreted and output the ruby code, ie display/print the result.
So it seems you need to use the extra = sign if you want to output in a standard ERB file.
<%= line_fetcher %>
Use <%= %> to output something in your view, so:
<%= line_fetcher %>
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.
This is the code in my logins_form.html.erb
<%= form_for(#login) do |f| %>
// code here
<%end%>
<%= form_tag(:controller=>'posts', :action=>'index') %>
// code here
<%end> --1
<%= form_tag(:controller=>'logins', :action=>'create') %>
// code here
<%end%> --2
It is only accepting one of the 1 or 2 not both. Why so? Even if I remove one of the two, both the forms are redirecting to logins.
What am I doing wrong?
Thanks.
Did you copy/paste your exact code?
If so look at your first form's end and you will notice your missing the % in the closing %>, which will cause the code to improperly compile the erb template.
You should be able to use two forms fine, as long as you are not trying to nest them within each other.
Would be interesting to see HTML output because you can't have nested forms on your page check question
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