How can I add link to my image on rails? - ruby-on-rails

I am trying to use link_to syntax on this line but nothing working out.
How can I do that in ruby on rails?
<%= image_tag post.picture.url if post.picture? %>

Use link_to do block
<%= link_to root_path do %>
<%= image_tag post.picture.url if post.picture? %>
<%= end %>

Try below code:
<% if post.picture? %>
<%= link_to "/path" do %>
<%= image_tag post.picture.url %>
<%= end %>
<% end %>

Try below
<%= link_to image_tag post.picture.url, navigate_to_path if post.picture? %>
OR
You can also try below if you need a block for additional things along with an image.
<%= link_to navigate_to_path do %>
<%= image_tag post.picture.url %>
<% end if post.picture? %>
Hope this helps.

Related

Unable to get the value of a variable inside Rails erb tags

I want to understand the erb usage. In the below code I am unable to figure out how to get the value of (group.id) in the if clause using the erb tags.
This probably has a very basic solution but I am unable to get proper answers.
The below code gives me syntax error.
<% current_user.favorite_groups.to_a.each do |group| %>
<%= if (group.id).newfavorite_texts.exists?(id: text.id) %>
<%= group.name %>
<%= link_to # do something %>
<% else %>
<%= group.name %>
<%= link_to # do something else %>
<% end %>
<% end %>
Thanks in advance.
You should be get going with the below code
<% current_user.favorite_groups.to_a.each do |group| %>
<% if group.newfavorite_texts.exists?(id: text.id) %>
<%= group.name %>
<%= link_to # do something %>
<% else %>
<%= group.name %>
<%= link_to # do something else %>
<% end %>
<% end %>

add link_to to image tag with instance

I have the code below and I want to add the link to, as it is with the Gravatar, to the first line. It should link to user.
<section class="user_info">
<% if #user.avatar.file? %>
<div class="s3_avatar"> <%= image_tag #user.avatar.url(:square) %></div>
<% else %>
<%= link_to((gravatar_for #user, size: 100), #user ) %>
<% end %>
<h1>
Instead of <%= image_tag #user.avatar.url(:square) %>
try the following:
<%= link_to user_path(#user) do %>
<%= image_tag #user.avatar.url(:square) %>
<% end %>
Refer to UrlHelper::link_to for more info.

How to I replace text with an image in a nav_link Ruby?

This is my nav link
<%= nav_link 'Loved', loved_properties_path %>
This is how i would usually insert an image
<%= image_tag 'desc.svg', class: "imgstar" %>
You can define as below,
<%= nav_link loved_properties_path do %>
<%= image_tag 'desc.svg', class: "imgstar" %>
<% end %>
Similar like link_to with blocks.
Just replace the argument:
<%= nav_link image_tag("desc.svg", class: "imgstar"), loved_properties_path %>
I encourage you to read some basic book about Ruby before proceeding.

How to add Link_to to an Uploaded Thumbnail

I'm trying to figure out the syntax to turn an uploaded project thumbnail in Ruby on Rails via Paperclip into a clickable link which leads to the projects details:
projects\index.html.erb
<% #projects.each do |project| %>
<%= image_tag (project.thumbnail.url(:small)) %>
<%= project.project_name %>
<%= project.project_city %>
<%= project.project_country %>
<%= link_to 'Show', project %>
Basically its adding the <%= link_to 'Show', project %> to the image_tag, but after trying a few iterations for the path I'm a bit stuck...
Thanks in advance
If I understood you correctly, this is what you want:
<%= link_to project do %>
<%= image_tag project.thumbnail.url(:small) %>
<% end %>

link_to in ruby on rails with additional html

I know there are more of these, but I couldn't find my answer as I'm still fairly new to RoR.
I need to take this:
<% if params[:forum_id] %>
<%= link_to "#{category.name}", category_path(category.id,:forum_id => params[:forum_id]) %>
<% else %>
<%= link_to "#{category.name}", category_path(category.id) %>
<% end %>
which prints out:
name
and I need:
<a href="mylink....">
<figure></figure>
<span>name</span>
</a>
Thanks!
You can use link_to as a block:
<%= link_to category_path(category_id) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
EDIT
The full solution:
<% if params[:forum_id] %>
<%= link_to category_path(category.id,:forum_id => params[:forum_id]) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
<% else %>
<%= link_to category_path(category.id) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
<% end %>

Resources