My original intention was to display some text on the image. At the same time, when we click the images, the webpage will be redirected.
And I use link_to function with div containing background image.
The code is like this:
<%= link_to raw('<div style="background-image:url(<%= image_url '1.jpg'%>);width:340px;"> This is a test</div>'),index_by_dvp_domain_path %>
But the system tells me there is SyntaxError.
You can pass link_to a block that contains the content you want to display. So instead of going with the link_to(display, url, options={}) you get link_to(url, option={}, &block) where you can do.
<%= link_to index_by_dvp_domain_path do %>
<div style="background-image: url(<%= image_url '1.jpg'%>);width:340px;">
This is a test
</div>
<% end %>
After you do this you can treat it like normal html.
As always, I'd recommend trying to move any style out into it's own separate stylesheet.
Best way to do it this is used following
<%= link_to index_by_dvp_domain_path do
content_tag(:div, 'This is a test',:style=>"background-image:url(#{image_url} '1.jpg');width:340px;" )
end
%>
OR
<%= link_to content_tag(:div, 'This is a test',:style=>"background-image:url(#{image_url} '1.jpg');width:340px;" ), index_by_dvp_domain_path %>
Please have a try with
<%= link_to raw('<div style="background-image:url(#{image_url '1.jpg'}%>);width:340px;"> This is a test</div>'),index_by_dvp_domain_path %>
I think using Link_to as below would be much more simpler even when you have a big block including multiple tags:
<%= link_to desired_path do %>
<div class="linkable">
<another div>
... some other tags
</another div>
</div>
<% end %>
and I recommend you to use a different background color for mouse over events because it shows the viewer that it's a link!
In you .css file:
.linkable:hover{
background-color: red;
}
Im so surprised to see that no one came up with the regular way of doing it in Rails.
<%= link_to image_tag("/images/1.jpg",:size => "340X340"),index_by_dvp_domain_path %>
Related
hey guys i have this piece of code
<%#=link_to raw page.body%></a></div>
<div class='col-md-8'>
<div class="container">
<h4><b> <%= link_to page.title, page_path(page.id)%> </b></h4>
<p><%=link_to raw page.body%></p>
</div>
</div>
<% end %>
i want to truncate the page.body output so after 200 text, it'll truncate it and add a read more button to view the full page.
How do i do it please
The ActionView::Helper::TextHelper#truncate might work for that:
<%= link_to truncate(page.body, length: 200) { link_to 'Read More', '#' } %>
The block passed permits you pass an additional link_to helper which you can make work with JS or any other as you need.
I am trying to underline words that are dynamically generated by the debug(params) method provided by rails. I have something below, but it obviously does not work, plus what I have below is attempt to try and change the words using methods that I already know about (like the .upcase method). I was hoping to underline the word controller if it appears in the text using only Ruby. Can anyone help me out here?
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<%= 'controller'.upcase %>
<% end %>
thanks
edit:
I should add that debug(params) is a method defined by RAILS, I was able to do the following which seems even more off, so far the answers have not been correct to what I want to do.
<% if Rails.env.development? %>
<% debug_method = debug(params).split.each do |word| %>
<% if word == 'controller:' %>
<ul><% word.upcase %></ul>
<% end %>
<% end %>
<%= debug_method.join %>
<% end %>
which returns the following text: https://ibb.co/cvnEpw , keep the answers coming in though. I want to get the words in the original box (that's generated by the method to underline the controller word https://ibb.co/jmSm2G).
use <u></u> tag
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<u><%= 'controller'.upcase %></u>
<% end %>
example here
Provide the css to generate html element:
p { text-decoration: underline; }
Add html elemnt to wrap your words:
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<p> <%= 'controller'.upcase %> </p>
<% end %>
The answer to the question is below. I had to use the .gsub and .html_safe methods.
<%= debug(params).gsub("controller:", "<u>controller:</u>").html_safe %>
This code keeps the existing html & css generated by rails intact
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've got a link_to that looks like this
<%= link_to site.name, site %>
I want to add a font icon from bootstrap into the anchor text, but when I try to use raw() for that, I can't figure out the syntax for including the site.name hook.
This is what I'm trying:
<%= link_to raw("<i class="icon-hdd"></i> site.name"), site %>
That's not working. How do I change that line to make it work?
Have you tried the do syntax?
<%= link_to site do %>
<i class="icon-hdd"> </i> <%= site.name%>
<% end %>
You need to interpolate site_name into the string.
<%= link_to raw("<i class='icon-hdd'></i> #{site.name}"), site %>
I have this tag: <%= link_to 'Show', user_listing_url(listing.user, listing) %> but instead of simply having it say 'Show' I actually want to place HTML inside of the <a> tag. Is this possible?
Example:
<div><div><img /></div></div>
yes you can pass a block to link_to
try something like this:
<%= link_to(user_listing_url(listing.user, listing)) do %>
<div><div><img/></div></div>
<% end %>
I totally recommend marflar's answer above.
However I would add one comment which is that if you are using html elements within a link_to block this may apply rails default link styling which may not be desirable.
One alternative is to use a button_to link but don't forget the default method for this is POST so specify the options as GET:
button_to(user_listing_url(listing.user, listing), method: :get) do %>
<div></div>
<% end %>