Home>

I am currently creating a posting app.
I would like to introduce active storage and carrierwave to enable posting of images and videos, but I am currently stuck with an error.

As for what I went to
・ Introduction of active storage
・ Description of has_one_attached
-Definition of information received by the controller
-Description to be displayed in the view (see error statement)
Will be.

I'm new to rails. Thanks for your support.

NoMethodError in Comments # index
Showing /Users/Desktop/debate-app/debate-app/app/views/comments/index.html.erb where line # 37 raised:
undefined method `image'for #<Comment :: ActiveRecord_AssociationRelation: 0x00007f8ce24cec00>
<% end%>
<% end%>
  <% if @ comments.image.attached?%><-This is the image here
    <% = image_tag @ commented.image%>
  <% end%>
  <% if @ comments.image.attached?%>
Corresponding source code
--comment model--
class Comment
--comments controller--
class CommentsController
Supplementary information (FW/tool version, etc.)

I'm sorry that the text is poor.
If i have any missing or interesting code, please leave a comment.

  • Answer # 1

        @comments = @ debate.comments.includes (: coach)

    Put binding.pry above here, @comments = @ debate.comments.includes (: coach)
    How about seeing if you can get the value with and if you can get the value with @ comments.image?

    Since the image is linked to one comment, I feel that this method at present will not work.

    If you do it with @comments

    @ comments.each do | comment |
    if comment.image.attached?

    I think that it will not work unless you put out one by one with each do etc. like this.