How to add Link_to to an Uploaded Thumbnail - ruby-on-rails

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 %>

Related

How can I add link to my image 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.

show action not working in nested show page

I have a weird issue that I can't seem to figure out nor know how to google.
Let me explain what the error is before going into the code stuff.
I have an album, which has many photos (photos belong to album).
Album works fine. Photo works fine.
When I try to click on show link of photo ... that's where things seem to go sideways, ie, none of it works after the first one.
Any ideas? Please let me know if further information is needed.
The problem seems to be coming from my Photos controller of show action, ie this:
# app/controllers/photos_controller.rb
def show
#album = Album.find(params[:album_id])
#photo = #album.photos.find(params[:id])
end
The error that is being generated is this:
ActiveRecord::RecordNotFound in PhotosController#show
Couldn't find Album with 'id'=4
The weird part is that the first photo of ablum works. It's all other subsequent photos that don't work.
This this the app/views/photos/show.html.erb:
<h1>Album Details</h1>
<b><p>Title</p></b>
<p><%= #album.title %></p>
<b><p>Descriptions</p></b>
<p><%= #album.description %></p>
<%= link_to "All Albums", albums_path %>
<h3>Photos</h3>
<% #album.photos.each do |photo| %>
<p>
<b>Title</b>
<%= photo.title %>
<%= link_to 'Show', album_photo_path(photo) %>
</p>
<% end %>
<h3>Add More Photos</h3>
<%= form_for([#album, #album.photos.build]) do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
It seems to me that you link_to for showing the photo is missing the id for the album.
Instead of
<%= link_to 'Show', album_photo_path(photo) %>
Should it be
<%= link_to 'Show', album_photo_path(#album, photo) %>
Since it looks like :album_id is expected as a separate parameter from the photos :id.

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.

Partial path error because of nil

I am working on a project and I just introduced polymorphism for posting text Posts and photo Posts.
When I try to post an image I get this error: "'nil' is not an ActiveModel-compatible object that returns a valid partial path."
The first show file (app/views/dashboards/show.html.erb) with code:
<%= form_for #text_post do |form| %>
<%= form.text_field :body, placeholder: 'Post content here' %>
<%= form.submit 'Post Title!' %>
<%end%>
<%= form_for #photo_post do |form| %>
<%= form.file_field :image %>
<%= form.submit 'Post Image!' %>
<%end%>
<%= render #posts %>
which leads to this at app/views/photo_posts/_photo_post.html.erb with the following code:
<%= image_tag photo_post.image.url(:post) %>
The app/views/posts/_post.html.erb which causes problem is:
<%= div_for post do %>
<%= link_to post.user.username, post.user %>
posted
<%= render post.content %>
<%= link_to time_ago_in_words(post.created_at), post %>
<% end %>
Sorry if the details I provide is insufficient, I am a new ROR developer. If you want, you can fork or check the project from here, (I pushed so you can check for the error).
Any help/guidance is greatly appreciated!
Please try
<%= render post.content if post.content %>
instead of
<%= render post.content %>
as per the error logs, this should fix the issue.

How to pass html for a link_to block?

I'm trying to pass a link_to block with html but can't get it. I tried some other ways with no luck so I will use my original code:
<% link_to survey_path(survey), :class => "button" do %>
<span>add questions to <%= survey.name %></span>
<% end %>
This doesn't show the :class though.
What needs to be corrected?
Try to add = to make it <%= %>
<%= link_to survey_path(survey), :class => "button" do %>
<span>add questions to <%= survey.name %></span>
<% end %>
In the view code in Rails 3 applications it’s sometimes necessary to
use <%= instead of <% at the beginning of blocks that output content,
such as form_for.
Since it's just a span, why don't you just do
<%= link_to "add questions to #{survey.name}", survey_path(survey), :class => "button" %>

Resources