Inserting HTML in "link name" field of link_to function - ruby-on-rails

I'm trying to convert an H3 header into a link. Here's the code of it
=link_to( "<H3>Home</H3>", root_url)
But it literally outputs the "Home" on my page. I'm using haml as view engine. Is there any way around?

Try this
=link_to(root_url) do
%h3 Home

Try
%h3= link_to("Home", root_url)

3rd solution is to use content_tag

Related

How to create a hyperlink without displaying the URL path

How do I create a hyperlink without displaying the URL path?
In my view code, I have something similar to this:
<% #users.each do |user| %>
<%= link_to user.lname, [:edit, user] %>
...
<% end %>
This code works, but produces something like:
Smith (url path)
I just want "Smith"
Not sure exactly what you have going on, or are going for with that array object as the URL target, but I'm betting this will solve your problems:
<%= link_to user.lname, edit_user_path(user) %>
Rails link_to is simply a Helper to create an HTML anchor tag.
You can see in the ActionView Helper Docs that link_to takes 3 parameters.
link_to(body, url, html_options = {})
This generates something similar to...
Smith
If you wish to change your routes, can you give more information like the full URL that's displaying, your routes.rb files, and the rake routes results?
Found the problem. It was in my lousy css. My css was placing the url in the title of the anchor text. I learned a lot about link_to also.

How To use dynamic link in haml

I am using Haml and have a header that contains a link to another page like so:
.heading
= link_to community.community_tag_path(community_tag) do
This ultimately renders a link.
I need to embed the same link as used in the header into a generic span tag like so:
%span View all Articles
How do I use this as a link using haml? Basically, I need the same link as in the header to work with the span
Yes you can pass a do block to the link_to function:
= link_to community.community_tag_path(community_tag) do
%span View all Articles
Or if you want the span to wrap the link_to:
%span= link_to "View all Articles", community.community_tag_path(community_tag)
To display the link with the community tag's name, simply do:
%span= link_to community_tag.name, community.community_tag_path(community_tag)
Here is some documentation about link_to: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Following the comments, to display links like "View all [community tag's name] articles", you can do a string interpolation:
"View all #{community_tag.name} articles"
And add this string as the first argument of the link_to.
You can achieve this by the following:
.heading
= link_to community.community_tag_path(community_tag) do
%span View all Articles
Also you can use a method inside of a string. But I prefer use directly like in the pasts comments.
%a{href: "#{community.community_tag_path(community_tag)}"} View all Articles

link_to syntax? want to hyperlink (variable.id + "string") together

link_to feed_item.votes_for + "Into it!", into_it_micropost_path(feed_item.id)
I basically want the hyperlink to be that variable next to the string "Into it!"
how can i achieve this? thanks
Actually it seems to me your version is supposed to work just fine.
Maybe try to put it in another way:
link_to "#{feed_item.votes_for} Into it!", into_it_micropost_path(feed_item.id)
Don't forget that for the link to appear you will have to put it inside <%= %>

How to find button with text in span?

I have this html-Output:
<span>Sign in</span>
How can I use:
click_button I18n.t 'users.sign_in'
I think capybara can not find the button because of the span. What is the best solution to find and test the button?
Cheers
Why don't you use rails tag for submit?
Try <%= submit_tag I18n.t('.users.sign_in') %> and I think it will work.
Well, that is the corresponding .erb-Code:
<%= link_to content_tag(:span,I18n.t("users.sign_in")), "#", style: "margin-left:132px; margin-top:12px;", class: "button-submit-magenta", onclick: "$(this).parents('form:first').submit()" %>
I need the 'span' for compatibility with IE7.
It's not working because you don't have a proper link or button. This will work if you only have one element with the same class on the page:
page.find('.button-submit').click
Try this. Hope it'll work.
page.find(:xpath, "//span[text()='Sign in']").click

How to display a rails link_to in a raw (or simple_format) text

I have an image model with an attribute :description which is a text, formatted as html text (e.g. with a few in the text), which I display in a view as follows:
simple_format(#image.description)
raw(#image.description) will do mainly the same I think. How can I add a link_to helper with a link into that text? I'm searching for something like the following text (which will be #image.description):
Text text text text.
Text text #{link_to "Text", #image.link)
#image.link will be the link. How can I do this?
Use ERB:
<%= raw ERB.new(#image.description).result(binding) %>
Wrap it in a helper method:
module ApplicationHelper
def simple_format(content)
ERB.new(content).result(binding).html_safe
end
end
And use it like:
<%= simple_format(#image.description) %>
Some example content you could use for your image description could be:
Check out <%= link_to "the first image", image_path(1) %>!
You can do this with this bit of code
link_to raw(#image.description), #image.link
If you need select only one random word:
words = #image.description.split
link_to raw(words.sample), #image.link
UPDATE:
For example when you create description you can add special symbols to word which you can use as link for example it can be brackets:
#image.description = This my description with link (Hello) you can follow it!
#image.description.gsub(/\(([^\)]+)\)/, link_to('\1', #image.link))
It will produce:
"This my description with link Hello you can follow it!"

Resources