Okay so here's a problem i've got 2 methods in my application_helper.rb
def sidebar_link_to(body, url, icon)
html_options = {class: "sidebar-menu-item"}
link_to url, html_options do
content_tag(:div, icon)
content_tag(:span, body)
end
end
and
def icon(filename, html_attributes = {})
begin
path = "public/icons/#{filename}.svg"
icon = Rails.root.join(path).read
html = Nokogiri::HTML::DocumentFragment.parse icon
svg = html.at_css "svg"
html_attributes.each { |name, value| svg[name] = value }
rescue Errno::ENOENT
html = "<!-- SVG #{path} not found -->"
end
raw html
end
and here's my html.erb code
<%= sidebar_link_to "Maps", maps_path, icon("outline/map", class: "h-8 w-8 text-white") %>
I would like for my icon to show next to Maps link but somehow it doesn't work any ideas?
to clarify i would like the outcome from helper to be this but i can't find solution:
<div class="sidebar-menu-item group flex items-center">
<%= icon "outline/map", class: "h-8 w-8 text-white"%>
<%= link_to "Maps", maps_path %>
</div>
Take a look at this, It is something related to your problem.
Related
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.
I try to create a very simple Rails helper to generate me a fancybox image preview. It should produce the following HTML:
<a href="path-to-image" class="fancybox">
<img src="path-to-image-thumb" ... />
</a>
However, to output a clickable gallery of many images, I need to set the rel attribute to the same value for all of the links:
<a href="path-to-image" class="fancybox" rel="my-gallery">
<img src="path-to-image-thumb" ... />
</a>
<a href="path-to-image2" class="fancybox" rel="my-gallery">
<img src="path-to-image2-thumb" ... />
</a>
I'd like to do this like the following:
# Single image
<%= image_preview(image) %>
# Image gallery
<%= image_gallery('my-gallery') do |g| %>
<%= g.image_preview(image) %>
<%= g.image_preview(image2) %>
<% end %>
So I want to use the same image_preview method both in a block and without one, but I'm struggling with the implementation.
My current implementation (which doesn't work) looks like this:
def image_preview(image, options = {})
link_to image.url, class: 'fancybox' do # How do I decide whether I have to put the rel attribute?
image_tag image.url(:thumb), options
end
end
def image_gallery(name, &block)
yield self # Somehow I have to use the provided name as rel attribute in image_preview...
end
I also tried my luck with with_options but didn't really get it to work for me (and sincerely don't exactly know how to use it for this case).
My implementation looks like this:
module ImageGalleryHelper
def image_gallery(name = nil, &block)
name ||= 'gallery_' + SecureRandom.hex(6)
content_tag :div, class: name do
with_options gallery_name: name do |gallery|
yield gallery
end
end
end
def fancy_image(image, options = {})
content_tag :a, href: image.url, class: 'fancybox', rel: options.delete(:gallery_name) do
image_tag image.url(:thumb), options
end
end
end
This does exactly what I want it to do. Maybe there are some enhancements possible?
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 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
I'm trying to create a simple partial that allows me to display code blocks without going through weird contortions in my code.
So I did this in the partial:
<% lang ||= "" %>
<% language = "lang='#{lang}'" %>
<div class="codebox">
<% if title %>
<h3><%= title %></h3>
<% end %>
<pre <%= language %>><%=text.unindent%></pre>
</div>
And this in lib for unindenting strings (thanks to a very nice SO suggestion):
class String
def unindent; gsub(/^#{scan(/^\s+/).min}/, "") end
end
Then, I can just do this, and get a very pretty little code box:
<%= render partial: 'pre', locals: { title: "example.html", lang: 'html', text: "
<div class='cl' style='text-align:center'>
<div class='collapse-group'>
<!-- Title, always viewable -->
<a class='bundle' href='#'>'Click here to expand'</a>
<div class='collapse'>
<!-- The content to be hidden or shown -->
</div>
</div>
</div>
"} %>
This turns into this:
Works like a charm, unless I put in a bunch of erb, in which case it goes nuts and starts erroring all over the place. Ex of an error-producer (contents not super relevant. I made sure that all quotes within are double, while the "string" is in single quotes):
<%= render partial: 'pre', locals: { title: "example.html", lang: 'html', text: '
<% sub ||= "" %>
<% term ||= "(expand)" %>
<% style ||= "" %>
<div class="cl" style="text-align:center">
<div class="collapse-group">
<<%=tag%> class="squeeze" style=<%="#{style}"%>>
<%=title%>
<% if sub != "" %>
<small><%= sub %></small>
<% end %>
<a class="bundle" href="#"><%= term %></a>
</<%=tag%>>
<div class="collapse">
' } %>
Any way I can just tell html that what I'm putting inside those quotes is 100% literal characters? I've tried individually escaping ">"s and ">"s and "%"s and all that, and it's a messy (and ineffectual) path I'm hoping not to go down.
EX of what I want the above to look like:
I think a nice approach in this would be to use #capture, for example in a helper (not tested, just a hint of what to do) :
def code_block( title = nil, lang = nil, &block )
output = capture( &block ) # this is the answer to all your problems
output = output.unindent # optional, escape it as you want, too
# rendering a partial is still possible,
# but i'd recommend using an absolute path :
render partial: 'my_html_bits/code_block',
locals: {title: title, lang: lang, text: output }
end
then you could do :
<%= code_block( 'example.html', 'html' ) do %>
<%# whatever code here will be captured %>
<p>Even plain old html.</p>
<% end %>
As a side note :
your #unindent method more or less mimicks an existing ActiveSupport monkey-patch on String, #strip_heredoc
in those cases, using #content_tag can also save you a lot of hassle, i.e :
<<%=tag%> class="squeeze" style=<%="#{style}"%>>
# more code...
could become :
<%= content_tag tag, class: 'squeeze', style: style do %>
# more code...