Outline of purpose

Always I am indebted.

What I want to achieve is that the original size image is displayed by clicking the uploaded (output/display) image (link).

HereOrHereRefer to the article in, get information such as id and URL of the uploaded image by array and hash, make a template of the image link using "link_to" and "image_tag", and describe only the path (URL) I was able to complete it, but I am struggling to embed each of the acquired URLs in the image.

I'm sorry for the dirty code, but I will attach the corresponding source code below.

Corresponding source code

↓ model

class Message</pre>
<p><br />
↓ controller</p>
<pre><code data-language = "Ruby">class MessagesController</pre>
<p><br />
↓ view</p>
<pre><code data-language = "Ruby">
    <% @ messages.each do | message |%>
            <% = message.message%>
------------------------- Click here for image output code ------------------- -----------------------------
            <% = link_to "#" do%>⇦ Enter the image path (or link) in the "#" part
              <% = image_tag message.image, class:'message-image' if message.image.attached?%>
            <% end%>
------------------------- This is the end of the image output code -------------------- -----------------------------
            <% = "Posted by # {message.user.nickname}"%>
            <% = l message.created_at%>
    <% end%>
What I tried

I put the following code in the "#" part of the view and checked the behavior, but it was not displayed well ....

・ @ Image_urls.find (id: message.id)
[email protected] Image_urls.find (id: message.id) .fetch (: image_url)
・ @ Image_urls.find (id: message.id) .image_url
・ @ Image_urls.find (id: message.id) [: image_url]
・ @ Image_urls.first (id: message.id)

When I checked it in the terminal, "#" was displayed as shown below, so search for it.HereI referred to the article, but I don't understand anything ...

[2] pry (#<MessagesController>)>@ image_urls.find (id: 3)
=>#<Enumerator: ...>

If anyone knows the solution, I would appreciate it if you could teach me.
Thank you.

* If i have any other necessary files or information, please let us know.

  • Answer # 1

    I think it was correct to use url_for (URL of image).
    So how about something like the following (while writing the if statement yourself)?

    <% = link_to url_for (message.image) do%>
    <% = image_tag message.image, class:'message-image' if message.image.attached?%>
    <% end%>