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>
Related
link_to(image_tag("icons/#{icon_name}.png"),url_or_object,options) I was trying use it like that but when I enter the project I'm seeing it like this http://prntscr.com/329yzz I can't see the image please help me,I am beginner in Ruby too
Please Help me
You are getting raw html output.
Use html_safe as shown below:
EDIT
As per OP's comments, html_safe was required in project_title_links method to convert the link returned from link_to_icon into an HTML safe string(DOM ready):
def link_to_icon(icon_name, url_or_object,options={})
link_to(image_tag("icons/#{icon_name}.png", ),url_or_object,options)
end
def project_title_links(project)
content_tag :h1 do [project.title, link_to_icon('show',project) ].join(' ').html_safe end
end
link_to takes an optional block, if you need anything more complicated than text:
link_to url_or_object, options do
image_tag("icons/#{icon_name}.png")
end
Since those method is being used within the views, link_to is using capture to evaluate the block. Hence it can be used like:
<%= link_to url_or_object, options do %>
<div class='wrapper'>
<label>Some text</label>
<%= image_tag("icons/#{icon_name}.png") %>
</div>
<% end %>
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.
I am trying to do something when I am connecting to my own server(local).
I found request.env from the website, so I am using that array to compare my IPs.
<%
if request.env['HTTP_HOST']!="127.0.0.1"
puts request.env['HTTP_HOST']
else
puts "its Local!"
end
%>
When I run above in rails3, I get nothing printed...
I am new to ruby&rails3..
When you want output in the web page, use <%= %>, not <% %>. The output will be the return value of the expression, so you don't want puts.
<%=
if request.env['HTTP_HOST']!="127.0.0.1"
request.env['HTTP_HOST']
else
"its Local!"
end
%>
Note that you can also use the local? method instead of checking environment directly.
<%=
if request.local?
"its Local!"
else
request.env['HTTP_HOST']
end
%>
If you like conciseness you can do it as one line:
<%= if request.local? then "its Local!" else request.env['HTTP_POST'] end %>
For even more view conciseness, make use of a helper method:
<%= ip_or_local %>
where in the matching view helper you put:
def ip_or_local
if request.local?
"its Local!"
else
request.env['HTTP_HOST']
end
end
For this simple case, it may be overkill but in general when you start seeing lots of code in your view, it's time to think about hiding certain things in helpers.
puts will write to the server in this case, not to the response. so you should look for your message in the log of the server.
Do you want to print to the log or to the view?
It might be clearer if you break things up into seperate erb tags.
<% if local? %>
<%= "Text for local" %>
<% else %>
<%= "Text for remote" %>
<% end -%>
You need to use <%= %> tags for lines you want printed and <% %> tags for lines that you want logic in, like conditionals.
If you're new to rails, you should check out the peepcode rails from scratch videos, they're pretty cheap and a lot of rails developers built their base on what's in them. Railscasts are also snack sized little tutorials that will easily get you through a lot of the basics.
Recommended reading:
RUBY:
The ruby pickaxe
The ruby way
Rails:
The Rails way
Head first ruby on rails
Hope I was of some help.
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.
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!