i have created in my application_helper a define for a standard link_to like this:
module ApplicationHelper
def foo_link_to(text, path)
out = "<span class=\"span\">"
out += link_to text, path
out += "</span>"
out
end
end
and in my partial i have
<%= foo_link_to 'text', home_path %>
but this is my output
<span class="span"><a href="/home">index</a></span>
now, my question is: where i need to insert the html_escape?
ty all
Thanks at all for the support.
Now i have another issue...if i wont this output what i have to do?
<i class="iclass"></i><span class="span"> text </span>
Using raw out and out.html_safe the output is
a href="/home">/home</a><span class="span"><i class="iclass">text</i></span>
Use raw, in your last line
raw out
And your helper can be further refactored as
def foo_link_to(text, path)
content_tag :span do
link_to text, path
end
end
I forget but it seems you don't need raw in the later case.
Update: For the last icon one, you can output like this
link_to 'home.html' do
content_tag :i, class: 'iclass'
content_tag :span, class: 'span' do
'Text'
end
end
I think it should be
module ApplicationHelper
def foo_link_to(text, path)
out = "<span class=\"span\">"
out += link_to text, path
out += "</span>"
out.html_safe
end
end
I think you should insert raw o html_safe in last line
html_safe can help you:
use out.html_safe
you can refer this
Related
My problem is that I can't get this helper tag to display at all.
So in application_helper.rb I want to have a <% nav_link(name, path) %> tag helper to append bootstrap's .active class dynamically.
My code is the following:
def nav_link(name, path)
content_tag(:li, :class => active_class(path)) do
link_to name, path
end
end
def active_class(path)
(current_page?(path) ? "active" : "").html_safe
end
and I would like to use it like so
<% nav_link("Users", users_path) %>
My hunch is that there's a variable somewhere that's not properly sanitized. How do I fix this? Is the html_safe call necessary?
Unless it's a typo, you should use <%= nav_link("Users", users_path) %>.
Without =, nothing will be displayed
I think that your problem is that you have written <% nav_link(name, path) %> this executes the code but doesn't print anything.
It should be <%= nav_link(name, path) %>
I highly recommend this Gem, it will do exactly what you want.
https://github.com/vigetlabs/nav_lynx
And here is a method it provides:
<%= nav_link_to 'Page', my_path, {}, { :wrapper => 'li' } %>
I have this in my helper:
def favorites_count(node)
content_tag :span, class: "card-favorite-count" do
content_tag :i, class: "icon-heart"
node.cached_votes_total
end
end
That is being called like this in the view:
<%= favorites_count(node) %>
And that renders this:
<span class="card-favorite-count"></span>
How do I get it to render the entire thing?
Edit 1
Per #tolgap's suggestion below, I tried this:
def favorites_count(node)
content_tag :span, class: "card-favorite-count" do
content_tag(:i, "" ,class: "icon-heart") + node.cached_votes_total
end
end
But that doesn't output the number value in node.cached_votes_total. It outputs everything else though, and in the correct semantic order. It is just the final part of this doesn't work quite yet.
So I figured out the answer. This is what the correct solution looks like:
def favorites_count(node)
content_tag :span, class: "card-favorite-count" do
concat(content_tag(:i, "" ,class: "icon-heart"))
concat(node.cached_votes_total)
end
end
Note that I had to use two concat() methods, because concat basically acts like a puts to the view. content_tag basically just returns the last line in the method to the view, so to override that I have to do that.
This comes from this article and this SO answer.
The last expression in the do block for a content_tag is what the content will be. So change it to:
def favorites_count(node)
content_tag :span, class: "card-favorite-count" do
node.cached_votes_total + content_tag(:i, class: "icon-heart")
end
end
So you concatenate those two together. Of course, you will need to do nil checks on node.cached_total_votes now.
I would like some pointers on how to rewrite the post to have links to hashtags. I need to have link_to( ..........) inside the post after I rewrite it and I can't really figure out how to do that in the backend application.
The current method I am trying to use is:
def twitify(tweet = '')
tweet.gsub!(/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/) do |tag|
link_to("##{tag}", '#')
end
end
However this only gives me:
jdawiodwiajdaw #mergiCaTeCrap (which is not a link)
This is the HTML:
<li id="tweet-<%= tweet.id %>">
<%= link_to gravatar_for(tweet.user, size: 50), tweet.user %>
<span class="user"><%= link_to tweet.user.name, tweet.user %></span>
<span class="content">
<%= twitify(tweet.content) %>
<%= image_tag tweet.picture.url if tweet.picture? %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(tweet.created_at) %> ago.
<% if current_user?(tweet.user) %>
<%= link_to "delete", tweet, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</span>
</li>
I would like it to not show me the href thing, how would I do that?
This is probably because Rails is escaping the html tags due to security reasons, in this case you just have to call the html_safe on the link_to like so:
def twitify(tweet = '')
tweet.gsub(/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/) do |tag|
" " + link_to("#{tag.strip}", '#')
end.html_safe
end
You should be good to go!
Okay first of all there is nothing wrong with the way you are going about it.
def twitify(tweet = '')
tweet.gsub(/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/) do |tag|
" " + link_to("#{tag.strip}", '#')
end
end
All I did was avoid modifying the original tweet (used gsub instead of the bang method gsub!), added a space before the #hash_tag,removed the extra # and space from the link.
As you can see
tweet = "This is a #tweet and it can make #hash_tags into #links"
twitify(tweet)
#=> "This is a #tweet and it can make #hash_tags into #links"
#your implementation
original_twitify(tweet)
#=> "This is a # #tweet and it can make # #hash_tags into # #links"
So I don't think there is really an issue here you pretty much had everything correct.
So I have this code that I want to convert to rails using link_to:
<a href="#">
<i class="icon-caret-right"></i>
<span>Home</span>
</a>
Giving a nice formatted space in between: > Home
the closest code to get the same result that I have tested with is:
<%= link_to "Factcars", root_path, class: "icon-caret-right" %>
Which gives me (without a space in between): >Home
<i class="icon-caret-right" ><%= link_to content_tag(:span, "Factcars"), root_path %></i>
Gives same result: >Home
Is there a correct way to format this code?
pass block in link_to
<%= link_to "#" do %>
<i class="icon-caret-right"></i>
<span>Home</span>
<% end %>
depending upon link and name , modify the above method.
navigation helper defines a simple method for link_to_with_icon in the module of Spree::Admin gem
#link_to_with_icon(icon_name, text, url, options = {}) ⇒ Object
you can redefine the method in your own helper
def link_to_with_icon(icon_name, text, url, options = {})
options[:class] = (options[:class].to_s + " fa fa-#{icon_name} icon_link with-tip").strip
options[:class] += ' no-text' if options[:no_text]
options[:title] = text if options[:no_text]
text = options[:no_text] ? '' : raw("<span class='text'>#{text}</span>")
options.delete(:no_text)
link_to(text, url, options)
end
I usually write helpers that way:
def bloco_vazio (texto = "", btn = "", args={})
titulo = content_tag :h3, "Vazio!"
p = content_tag :p, texto
content_tag :div, (titulo + tag(:hr) + p + btn ), args
end
But i commonly see people using other approaches, like:
def flash_notice
html = ""
unless flash.empty?
flash.each do |f|
html << "<div class='alert alert-#{f[:type].to_s}'>"
html << "<a class='close' data-dismiss='alert'>×</a>"
html << f[:text].to_s
html << "</div>"
end
end
html
end
or
def a_helper (some_text ="")
%{ <h3>some title</h3>
<p>#{some_text}</p>
}%
end
I used these two lasts in the past and ran into some problems then started using the content_tag and tag helpers, even that i still have to use the .html_safe method sometimes.
Is there a standard way to build helpers?
If html is longer than 1 line, i usually put the html in a partial and call it with a custom helper method
view
<= display_my_html(#item, {html_class: "active"}) %>
helper
def display_my_html(item, opts={})
name = item.name.upcase
html_class = opts.key?(:html_class) ? opts[:html_class] : "normal"
render "my_html", name: name, html_class: html_class
end
partial
<div class="<%= html_class %>">
<span class="user">
<%= name %>
</span>
</div>
The best approach is to write ruby code in the helpers and html only on .html.erb files, even if thay are "strings", so you should use the content_tag for helpers and if you want one block you could use:
<%= content_tag :div, :class => "strong" do -%>
Hello world!
<% end -%>
and if your html is big consider transfering to a partial, hope it elucidates your doubt.
If you want to build a longer "safe" html the recommended way is:
html = "".html_safe
html.safe_concat "Testing boldfaced char <b>A</b>"
html
Actually, you can and should make use of partials in view helpers as well. Having raw html tags outside of views is a code smell that I try to avoid as much as possible.
Related question: Rails view helpers in helper file