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
Related
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.
I have an image being pulled from the database in a table, that I would then like to make into a link so the viewer can be taken to the full article. I have seen that in theory I could use the below,
<%= link_to image_tag('image/someimage.png') + "Some text", some_path %>
but in my case 'image/someimage.png' is
<%= image_tag coffeeshop.image_thumb_path %>
I've tried simply dropping the image tag section in, so it becomes <%= link_to image_tag('image_tag coffeeshop.image_thumb_path') + "Some text", some_path %> but this doesn't work. Is there a way to do this?
link_to(body, url, html_options = {})
The first argument in your link_to will be interpreted as the body of the tag, and then your url, but if you want to add more content inside the link_to you can open it and then close it, all inside will be interpreted this way:
<a href="#">
...
</a>
So you can try:
<%= link_to some_path do %>
<%= image_tag coffeeshop.image_thumb_path %>
<% end %>
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've got a nav like this:
<nav>
<ul class='nav nav-pills'>
<li>
<%= link_to 'link1', '#' %>
</li>
<li>
<%= link_to 'link1', '#' %>
</li>
</ul>
</nav>
My routes, view and controller are set up such that root_url/.../action and root_url/.../action/:page_id will both render the same view, with an instance variable #page being set based on the :page_id param or to a specified default for the action's root. Later in the view, I'm rendering a partial that matches the name of #page.
What I'm trying to do is set class='active' on the <li> whose link text matches the value of #page.
My original inclination was to stay DRY and set window.page_id to match #page and use CoffeeScript to add the class, but that gave me a very noticeable delay between the page loading and the class being set.
Does anyone know the best method of accomplishing this? Right now I'm putting embedded ruby in each one of the <li> elements, which is rather undesirable.
Borrowed and slightly modified to fit your needs from another S.O. post Best way to add "current" class to nav in Rails 3
def nav_link(link_text, page)
class_name = link_text == page ? 'active' : ''
content_tag(:li, :class => class_name) do
link_to link_text, page
end
end
used like:
nav_link 'Home', #page
Expanding on Zeiv's answer, you can just compare the url:
Helper Method:
def nav_link(link_text, link_path)
content_tag(:li, class: ('active' if link_path == url_for(only_path: true)) ) do
link_to link_text, link_path
end
end
Now in the view you can do this:
<%= nav_link "Some Page", some_page_path %>
Expanding on 99miles's answer, I put this in the corresponding helper:
def nav_link(link_text, link_path, current_page)
content_tag(:li, class: ('active' if link_text.downcase.gsub(' ', '_') == current_page) ) do
link_to link_text, link_path
end
end
That way <li> elements that aren't active don't have an empty class, and it works even when the link text contains spaces.
Used in the view like:
<%= nav_link "My Page", page_path('my_page'), #page %>
Edit:
Or, if you want to specify the text of the link independently from the :page_id
def nav_link(link_text, link_path, page_id, current_page)
content_tag(:li, class: ('active' if page_id == current_page) ) do
link_to link_text, link_path
end
end
Then you can do
<%= nav_link "My Link", page_path('my_page'), 'my_page', #page %>
basically I want the following output with link_to:
<i class="some-class"></i>Some text
Any suggestions?
<%= link_to "#" do %>
<i class="some-class"></i>Some text
<% end %>
Block form of link_to for the win!
What about content_tag ?
link_to "#{content_tag(:i, "", :class => 'some-class')}Some text".html_safe, root_url