Here is my code
Here i want to include youtube video id dynamically. Its not taken here Kindly give me a solution
<%= link_to image_tag("http://img.youtube.com/vi/<%= video.provider_uid %>/hqdefault.jpg"), "http://www.facebook.com" %>
You need string interpolation here, not ERB tag:
<%= link_to image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg"), "http://www.facebook.com" %>
I would rewrite the code like that:
<%= link_to "http://www.example.com" do %>
<%= image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg") %>
<% end %>
I found a solution
<%= link_to image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg"), "http://www.example.com" %>
this is the code
Related
I am using ruby 2 and rails 4. I want to add http link into image link in rails. How can I create that?
My codes:
<% for g in #pictures %>
<%= link_to image_tag g.pic_url, class: "img-responsive img-thumbnail" %>
<% end %>
I want to create something like below using rails.
<img src="/assets/image_001.jpg" class="img-responsive img-thumbnail">
Please share with me if any one has any idea.
The link_to helper can take a block of code, allowing you to do something like the following:
<% for g in #pictures %>
<%= link_to g.pic_url do %>
<%= image_tag g.pic_url, class: "img-responsive img-thumbnail" %>
<% end %>
<% end %>
More info on the link_to helper can be found by looking through the Rails API documentation.
Hope it helps!
my solution:
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some css class here" %>
<% end %>
link_to take first argument as link's name and second argument is as url. in your code you have not assign url option. Change it as below
Try:
<%= link_to (image_tag(g.pic_url, class: "img-responsive img-thumbnail"), g.pic_url) %>
Ruby on Rails using link_to with image_tag
Hi I have included given code in my view
<% if employee.profile.present? %>
<%= employee.profile.name %>
<% end %>
Please guide me how to dry this code
Two ways I would suggest.
<%= employee.profile.try(:name) %>
<%= employee.profile.name if employee.profile %>
An other good solution is to use an helper:
<%= has_profile(employee) %>
def has_profile(employee)
employee.profile.name if employee.profile.present?
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 %>
Old usage:
<% form_tag %>
...
<% end %>
<% form_for %>
....
<% end %>
New usage:
<%= form_tag %>
...
<% end %>
<%= form_for %>
....
<% end %>
(I was learning Head First Rails and know the differences in this list)
I knew that scriptlet don't need = in the <>, but if <%= form_tag %> is not a scriptlet, why does it need a <% end %>?
Does anyone have ideas about this?
Firstly, I've got no idea what a 'scriplet' is; anyway, this was a change in rails 3.0 - see the release notes: http://guides.rubyonrails.org/3_0_release_notes.html#helpers-with-blocks.
Rails 3 brings overall consistency in the API and in this case in the view API.
Rule is:
(want_to_display?) ? (use =) : (don't use =)
How would i use
<% link_to (post) do %>
<%= pluralize(post.comments.size,'comment') %>
<% end %>
to link to a div, for example the url:
http://myblog.com/post/21#mydiv
i need to do this so that when a user clicks the comments link they are taken to the comments div on the page. This would also be useful to use to redirect users to the comment they have just posted like so:
http://myblog.com/post/21#comment_id
thanks alot!
You can use:
<% link_to(post, :anchor => 'mydiv') do %>
<%= pluralize(post.comments.size,'comment') %>
<% end %>
link_to API documentation
link_to(post, pluralize(post.comments.size,'comment'), :anchor => "mydiv")