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")
Related
I'm using Rails 4.2.7. I have this link in the header of my application
<%= link_to 'Edit', edit_users_path %>
How would I not render this link if I'm already on the edit_users_path page?
You can put the condition at the end of the erb fragment
<%= link_to('Edit', edit_users_path) unless current_page? %>
just call
<% if current_page?(edit_users_path) %>
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
I came from a JS background so Rails is weird to me. I currently have a show.html.erb for the contest model:
<h1>Contest name: <%= #contest.name %></h1>
<h2>Contest criteria: <%= #contest.criteria %></h2>
<h3>Photos: </h3>
<%= link_to "Enter Contest", "#" %>
<% #contest.photos.each do |photo| %>
<%= image_tag("#{photo}") %>
<% end %>
With the link_to I'm trying to render all photos that belongs to the current_user and pick one of them to assign it to the current contest. The params passing seems all so mysterious to me. Can you guys point me in the right direction of how I should tackle this problem? Thanks
What I understand you need to select an image from list of images and pass the id. So, try this
<%= label_tag "Enter Contest" %>
<% #contest.photos.each do |photo| %>
<%= link_to (image_tag("#{some photo path}")), some_controller_method_path(:photo_id => photo.id) %>
<% end %>
And in controller you can use params[:photo_id]
I've got a very simple setup of posts with associated tags. When I 'show' a post I want to be able to link to each one of those tags BUT it seems to only link to the tag :id that shares the :id of the post I'm showing.
My code:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path() %>
<% end %>
Let's say I'm looking at post number 2, the above will only link me to /tags/2 , no matter which tag I click on. I'm sure the answer is embarrassingly simple but it's driving me crazy. Thanks so much.
Pass tag to your route helper:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>
Update:
Change tag_list to tags:
<% #post.tags.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>
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