How To use dynamic link in haml - ruby-on-rails

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

Related

Set id for text inside link_to in rails

I have a dropdown list having links to specific categories, in each line of category I want it to include name of category and number of books of that category. Anyway, I want the number part having a different style in comparison with name part.
Here is the code for each one:
%li= link_to "#{category.name} (#{category.books.count})" , root_path(category_id: category.id)
I want to set id for #{category.name} and (#{category.books.count}), what should I do?
You should define the attribute id. In your code, you should do it like:
%li= link_to "#{category.name} (#{category.books.count})" , root_path(category_id: category.id), id: "#{category.name} (#{category.books.count})"
I found the answer, we should use link_to do like this:
= link_to root_path(category_id: category.id, per_page: params[:per_page]) do
.row.cate-display
#cate-name= category.name
#cate-count= "(#{category.books.count})"

Rails Display Link in Database Using link_to

In Rails, I have a "notifications" class, one field of which is "link". The links contained within this class are formatted like: exchange_path(6), where that is the path to the show action in the exchange controller.
I'm now trying to output this link as such:
<%= link_to "View Exchange", notification.link %>
This line is in a loop which begins as such:
<% #notifications.each do |notification| %>
When I click this link, it takes me to localhost:3000/users/exchange_path(6) instead of localhost:3000/exchanges/6 like I would expect. (The loop generating the faulty link is on localhost:3000/users/2)
this could be scary...
<%= link_to "View Exchange", eval(notification.link) %>
should evaluate and use the path helpers. but you need to be 100% sure that nothing bad gets put in the link field..
You could do this:
<%= link_to("View Exchange", "/#{notification.link.gsub('(', '/').gsub(')', '').gsub('_path', 's')}") %>
or set up a method in your model that formats it for you:
def format_link
link.gsub('(', '/').gsub(')', '').gsub('_path', 's')
end
and just call that in your link_to:
link_to("View Exchanges", notification.format_link)
This will only work if all the links are formatted exactly as the example in the question

Rails, link_to helper with an URL as a param

I want to generate the next html link:
http://url.com
To reproduce it using the link_to helper I have to write:
<%= link_to "http://url.com", "http://url.com" %>
What doesn't look DRY at all, I was expecting this to work:
<%= link_to "http://url.com" %>
But the above code generate a link targeting the actual request.url, not the one I'm sending in the param.
Am I missing something?
You're not missing anything --- the normal case is for the URL and the text that shows to the user to be different.
If you'd like, you could create a helper like
def link_to_href(link, args={})
link_to link, link, args
end
then, when you use it,
<%= link_to_href "http://url.com" %>
Will output
http://url.com
If you take a look at the source code of link_to you will see that at line 248 the a tag label is build with name || url.
That's why you have this behaviour and there is noway to do it like you're expecting.

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!"

Inserting HTML in "link name" field of link_to function

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

Resources